ia64/xen-unstable

changeset 16167:7231d971f78c

hvm: Fix PCI-passthru string parsing.
Fixes crash of xend during HVM domain ccreation.
Signed-off-by: Keir Fraser <keir@xensource.com>
author Keir Fraser <keir@xensource.com>
date Sat Oct 20 09:30:00 2007 +0100 (2007-10-20)
parents 6df47366830c
children 328dcc446f9e 3b481059a54d
files tools/ioemu/hw/pass-through.c tools/python/xen/lowlevel/xc/xc.c
line diff
     1.1 --- a/tools/ioemu/hw/pass-through.c	Fri Oct 19 18:00:10 2007 +0100
     1.2 +++ b/tools/ioemu/hw/pass-through.c	Sat Oct 20 09:30:00 2007 +0100
     1.3 @@ -31,48 +31,28 @@ extern FILE *logfile;
     1.4  
     1.5  static int token_value(char *token)
     1.6  {
     1.7 -    token = strchr(token, 'x');
     1.8 -    token = token + 1;
     1.9 -
    1.10 -    return ((int) strtol(token, NULL, 16));
    1.11 +    token = strchr(token, 'x') + 1;
    1.12 +    return strtol(token, NULL, 16);
    1.13  }
    1.14  
    1.15 -static int first_bdf(char *pci_str, char **last,
    1.16 -                     int *seg, int *bus, int *dev, int *func)
    1.17 +static int next_bdf(char **str, int *seg, int *bus, int *dev, int *func)
    1.18  {
    1.19      char *token;
    1.20  
    1.21 -    token = strtok_r(pci_str, ",", last);
    1.22 +    token = strchr(*str, ',');
    1.23      if ( !token )
    1.24          return 0;
    1.25 +    token++;
    1.26  
    1.27      *seg  = token_value(token);
    1.28 -    token = strtok_r(NULL, ",", last);
    1.29 +    token = strchr(token, ',') + 1;
    1.30      *bus  = token_value(token);
    1.31 -    token = strtok_r(NULL, ",", last);
    1.32 +    token = strchr(token, ',') + 1;
    1.33      *dev  = token_value(token);
    1.34 -    token = strtok_r(NULL, ",", last);
    1.35 +    token = strchr(token, ',') + 1;
    1.36      *func  = token_value(token);
    1.37  
    1.38 -    return 1;
    1.39 -}
    1.40 -
    1.41 -static int next_bdf(char **last, int *seg, int *bus, int *dev, int *func)
    1.42 -{
    1.43 -    char *token;
    1.44 -
    1.45 -    token = strtok_r(NULL, ",", last);
    1.46 -    if ( !token )
    1.47 -        return 0;
    1.48 -
    1.49 -    *seg  = token_value(token);
    1.50 -    token = strtok_r(NULL, ",", last);
    1.51 -    *bus  = token_value(token);
    1.52 -    token = strtok_r(NULL, ",", last);
    1.53 -    *dev  = token_value(token);
    1.54 -    token = strtok_r(NULL, ",", last);
    1.55 -    *func  = token_value(token);
    1.56 -
    1.57 +    *str = token;
    1.58      return 1;
    1.59  }
    1.60  
    1.61 @@ -422,8 +402,6 @@ int pt_init(PCIBus *e_bus, char *direct_
    1.62      int seg, b, d, f;
    1.63      struct pt_dev *pt_dev;
    1.64      struct pci_access *pci_access;
    1.65 -    int get_bdf;
    1.66 -    char *last = NULL;
    1.67  
    1.68      /* Initialize libpci */
    1.69      pci_access = pci_alloc();
    1.70 @@ -436,9 +414,7 @@ int pt_init(PCIBus *e_bus, char *direct_
    1.71      pci_scan_bus(pci_access);
    1.72  
    1.73      /* Assign given devices to guest */
    1.74 -    for ( get_bdf = first_bdf(direct_pci, &last, &seg, &b, &d, &f);
    1.75 -          get_bdf;
    1.76 -          get_bdf = next_bdf(&last, &seg, &b, &d, &f) )
    1.77 +    while ( next_bdf(&direct_pci, &seg, &b, &d, &f) )
    1.78      {
    1.79          /* Register real device with the emulated bus */
    1.80          pt_dev = register_real_device(e_bus, "DIRECT PCI", PT_VIRT_DEVFN_AUTO,
     2.1 --- a/tools/python/xen/lowlevel/xc/xc.c	Fri Oct 19 18:00:10 2007 +0100
     2.2 +++ b/tools/python/xen/lowlevel/xc/xc.c	Sat Oct 20 09:30:00 2007 +0100
     2.3 @@ -531,48 +531,28 @@ static PyObject *pyxc_set_hvm_param(XcOb
     2.4  
     2.5  static int token_value(char *token)
     2.6  {
     2.7 -    token = strchr(token, 'x');
     2.8 -    token = token + 1;
     2.9 -
    2.10 -    return ((int) strtol(token, NULL, 16));
    2.11 +    token = strchr(token, 'x') + 1;
    2.12 +    return strtol(token, NULL, 16);
    2.13  }
    2.14  
    2.15 -static int first_bdf(char *pci_str, char **last,
    2.16 -                     int *seg, int *bus, int *dev, int *func)
    2.17 +static int next_bdf(char **str, int *seg, int *bus, int *dev, int *func)
    2.18  {
    2.19      char *token;
    2.20  
    2.21 -    token = strtok_r(pci_str, ",", last);
    2.22 +    token = strchr(*str, ',');
    2.23      if ( !token )
    2.24          return 0;
    2.25 +    token++;
    2.26  
    2.27      *seg  = token_value(token);
    2.28 -    token = strtok_r(NULL, ",", last);
    2.29 +    token = strchr(token, ',') + 1;
    2.30      *bus  = token_value(token);
    2.31 -    token = strtok_r(NULL, ",", last);
    2.32 +    token = strchr(token, ',') + 1;
    2.33      *dev  = token_value(token);
    2.34 -    token = strtok_r(NULL, ",", last);
    2.35 +    token = strchr(token, ',') + 1;
    2.36      *func  = token_value(token);
    2.37  
    2.38 -    return 1;
    2.39 -}
    2.40 -
    2.41 -static int next_bdf(char **last, int *seg, int *bus, int *dev, int *func)
    2.42 -{
    2.43 -    char *token;
    2.44 -
    2.45 -    token = strtok_r(NULL, ",", last);
    2.46 -    if ( !token )
    2.47 -        return 0;
    2.48 -
    2.49 -    *seg  = token_value(token);
    2.50 -    token = strtok_r(NULL, ",", last);
    2.51 -    *bus  = token_value(token);
    2.52 -    token = strtok_r(NULL, ",", last);
    2.53 -    *dev  = token_value(token);
    2.54 -    token = strtok_r(NULL, ",", last);
    2.55 -    *func  = token_value(token);
    2.56 -
    2.57 +    *str = token;
    2.58      return 1;
    2.59  }
    2.60  
    2.61 @@ -584,17 +564,13 @@ static PyObject *pyxc_assign_device(XcOb
    2.62      char *pci_str;
    2.63      uint32_t bdf = 0;
    2.64      int seg, bus, dev, func;
    2.65 -    int get_bdf;
    2.66 -    char *last = NULL;
    2.67  
    2.68      static char *kwd_list[] = { "domid", "pci", NULL };
    2.69 -    if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|s", kwd_list,
    2.70 +    if ( !PyArg_ParseTupleAndKeywords(args, kwds, "is", kwd_list,
    2.71                                        &dom, &pci_str) )
    2.72          return NULL;
    2.73  
    2.74 -    for ( get_bdf = first_bdf(pci_str, &last, &seg, &bus, &dev, &func);
    2.75 -          get_bdf;
    2.76 -          get_bdf = next_bdf(&last, &seg, &bus, &dev, &func) )
    2.77 +    while ( next_bdf(&pci_str, &seg, &bus, &dev, &func) )
    2.78      {
    2.79          bdf |= (bus & 0xff) << 16;
    2.80          bdf |= (dev & 0x1f) << 11;