ia64/xen-unstable

changeset 15937:177ebf350b4c

libxc portability fixes for NetBSD.

- use MAP_ANON, that is what both (BSD-)Unix and Linux have
- change last_error handling to use pthreads
- round mlock() parameters to page alignment
- cleanup: No need to include <xen/sys/privcmd.h>
a second time in xg_private.h

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Wed Sep 19 15:42:56 2007 +0100 (2007-09-19)
parents b21ba95c686b
children fdabc0af5852 4c8394e3b011
files tools/libxc/xc_dom_core.c tools/libxc/xc_private.c tools/libxc/xg_private.h
line diff
     1.1 --- a/tools/libxc/xc_dom_core.c	Wed Sep 19 15:20:56 2007 +0100
     1.2 +++ b/tools/libxc/xc_dom_core.c	Wed Sep 19 15:42:56 2007 +0100
     1.3 @@ -122,7 +122,7 @@ void *xc_dom_malloc_page_aligned(struct 
     1.4      memset(block, 0, sizeof(*block));
     1.5      block->mmap_len = size;
     1.6      block->mmap_ptr = mmap(NULL, block->mmap_len,
     1.7 -                           PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
     1.8 +                           PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON,
     1.9                             -1, 0);
    1.10      if ( block->mmap_ptr == MAP_FAILED )
    1.11      {
    1.12 @@ -354,7 +354,7 @@ void *xc_dom_pfn_to_ptr(struct xc_dom_im
    1.13      {
    1.14          mode = "anonymous memory";
    1.15          phys->ptr = mmap(NULL, phys->count << page_shift,
    1.16 -                         PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
    1.17 +                         PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON,
    1.18                           -1, 0);
    1.19          if ( phys->ptr == MAP_FAILED )
    1.20          {
     2.1 --- a/tools/libxc/xc_private.c	Wed Sep 19 15:20:56 2007 +0100
     2.2 +++ b/tools/libxc/xc_private.c	Wed Sep 19 15:42:56 2007 +0100
     2.3 @@ -10,7 +10,12 @@
     2.4  #include <stdarg.h>
     2.5  #include <pthread.h>
     2.6  
     2.7 -static __thread xc_error last_error = { XC_ERROR_NONE, ""};
     2.8 +static pthread_key_t last_error_pkey;
     2.9 +static pthread_once_t last_error_pkey_once = PTHREAD_ONCE_INIT;
    2.10 +
    2.11 +static pthread_key_t errbuf_pkey;
    2.12 +static pthread_once_t errbuf_pkey_once = PTHREAD_ONCE_INIT;
    2.13 +
    2.14  #if DEBUG
    2.15  static xc_error_handler error_handler = xc_default_error_handler;
    2.16  #else
    2.17 @@ -23,15 +28,45 @@ void xc_default_error_handler(const xc_e
    2.18      fprintf(stderr, "ERROR %s: %s\n", desc, err->message);
    2.19  }
    2.20  
    2.21 +static void
    2.22 +_xc_clean_last_error(void *m)
    2.23 +{
    2.24 +    free(m);
    2.25 +    pthread_setspecific(last_error_pkey, NULL);
    2.26 +}
    2.27 +
    2.28 +static void
    2.29 +_xc_init_last_error(void)
    2.30 +{
    2.31 +    pthread_key_create(&last_error_pkey, _xc_clean_last_error);
    2.32 +}
    2.33 +
    2.34 +static xc_error *
    2.35 +_xc_get_last_error(void)
    2.36 +{
    2.37 +    xc_error *last_error;
    2.38 +
    2.39 +    pthread_once(&last_error_pkey_once, _xc_init_last_error);
    2.40 +
    2.41 +    last_error = pthread_getspecific(last_error_pkey);
    2.42 +    if (last_error == NULL) {
    2.43 +        last_error = malloc(sizeof(xc_error));
    2.44 +        pthread_setspecific(last_error_pkey, last_error);
    2.45 +    }
    2.46 +
    2.47 +    return last_error;
    2.48 +}
    2.49 +
    2.50  const xc_error *xc_get_last_error(void)
    2.51  {
    2.52 -    return &last_error;
    2.53 +    return _xc_get_last_error();
    2.54  }
    2.55  
    2.56  void xc_clear_last_error(void)
    2.57  {
    2.58 -    last_error.code = XC_ERROR_NONE;
    2.59 -    last_error.message[0] = '\0';
    2.60 +    xc_error *last_error = _xc_get_last_error();
    2.61 +    last_error->code = XC_ERROR_NONE;
    2.62 +    last_error->message[0] = '\0';
    2.63  }
    2.64  
    2.65  const char *xc_error_code_to_desc(int code)
    2.66 @@ -61,12 +96,12 @@ xc_error_handler xc_set_error_handler(xc
    2.67      return old;
    2.68  }
    2.69  
    2.70 -
    2.71  static void _xc_set_error(int code, const char *msg)
    2.72  {
    2.73 -    last_error.code = code;
    2.74 -    strncpy(last_error.message, msg, XC_MAX_ERROR_MSG_LEN - 1);
    2.75 -    last_error.message[XC_MAX_ERROR_MSG_LEN-1] = '\0';
    2.76 +    xc_error *last_error = _xc_get_last_error();
    2.77 +    last_error->code = code;
    2.78 +    strncpy(last_error->message, msg, XC_MAX_ERROR_MSG_LEN - 1);
    2.79 +    last_error->message[XC_MAX_ERROR_MSG_LEN-1] = '\0';
    2.80  }
    2.81  
    2.82  void xc_set_error(int code, const char *fmt, ...)
    2.83 @@ -84,23 +119,29 @@ void xc_set_error(int code, const char *
    2.84  
    2.85      errno = saved_errno;
    2.86  
    2.87 -    if ( error_handler != NULL )
    2.88 -        error_handler(&last_error);
    2.89 +    if ( error_handler != NULL ) {
    2.90 +        xc_error *last_error = _xc_get_last_error();
    2.91 +        error_handler(last_error);
    2.92 +    }
    2.93  }
    2.94  
    2.95  int lock_pages(void *addr, size_t len)
    2.96  {
    2.97        int e = 0;
    2.98  #ifndef __sun__
    2.99 -      e = mlock(addr, len);
   2.100 +      void *laddr = (void *)((unsigned long)addr & PAGE_MASK);
   2.101 +      size_t llen = (len + PAGE_SIZE - 1) & PAGE_MASK;
   2.102 +      e = mlock(laddr, llen);
   2.103  #endif
   2.104 -      return (e);
   2.105 +      return e;
   2.106  }
   2.107  
   2.108  void unlock_pages(void *addr, size_t len)
   2.109  {
   2.110  #ifndef __sun__
   2.111 -    safe_munlock(addr, len);
   2.112 +    void *laddr = (void *)((unsigned long)addr & PAGE_MASK);
   2.113 +    size_t llen = (len + PAGE_SIZE - 1) & PAGE_MASK;
   2.114 +    safe_munlock(laddr, llen);
   2.115  #endif
   2.116  }
   2.117  
   2.118 @@ -466,20 +507,42 @@ unsigned long xc_make_page_below_4G(
   2.119      return new_mfn;
   2.120  }
   2.121  
   2.122 +static void
   2.123 +_xc_clean_errbuf(void * m)
   2.124 +{
   2.125 +    free(m);
   2.126 +    pthread_setspecific(errbuf_pkey, NULL);
   2.127 +}
   2.128 +
   2.129 +static void
   2.130 +_xc_init_errbuf(void)
   2.131 +{
   2.132 +    pthread_key_create(&errbuf_pkey, _xc_clean_errbuf);
   2.133 +}
   2.134 +
   2.135  char *safe_strerror(int errcode)
   2.136  {
   2.137 -    static __thread char errbuf[32];
   2.138 +#define XS_BUFSIZE 32
   2.139 +    char *errbuf;
   2.140      static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
   2.141      char *strerror_str;
   2.142  
   2.143 +    pthread_once(&errbuf_pkey_once, _xc_init_errbuf);
   2.144 +
   2.145 +    errbuf = pthread_getspecific(errbuf_pkey);
   2.146 +    if (errbuf == NULL) {
   2.147 +        errbuf = malloc(XS_BUFSIZE);
   2.148 +        pthread_setspecific(errbuf_pkey, errbuf);
   2.149 +    }
   2.150 +
   2.151      /*
   2.152       * Thread-unsafe strerror() is protected by a local mutex. We copy
   2.153       * the string to a thread-private buffer before releasing the mutex.
   2.154       */
   2.155      pthread_mutex_lock(&mutex);
   2.156      strerror_str = strerror(errcode);
   2.157 -    strncpy(errbuf, strerror_str, sizeof(errbuf));
   2.158 -    errbuf[sizeof(errbuf)-1] = '\0';
   2.159 +    strncpy(errbuf, strerror_str, XS_BUFSIZE);
   2.160 +    errbuf[XS_BUFSIZE-1] = '\0';
   2.161      pthread_mutex_unlock(&mutex);
   2.162  
   2.163      return errbuf;
     3.1 --- a/tools/libxc/xg_private.h	Wed Sep 19 15:20:56 2007 +0100
     3.2 +++ b/tools/libxc/xg_private.h	Wed Sep 19 15:42:56 2007 +0100
     3.3 @@ -15,7 +15,6 @@
     3.4  #include "xenguest.h"
     3.5  #include "xc_private.h"
     3.6  
     3.7 -#include <xen/sys/privcmd.h>
     3.8  #include <xen/memory.h>
     3.9  #include <xen/elfnote.h>
    3.10