[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen-unstable] libxc portability fixes for NetBSD.



# HG changeset patch
# User kfraser@xxxxxxxxxxxxxxxxxxxxx
# Date 1190212976 -3600
# Node ID 177ebf350b4c37a5ed83ac1475d2ebd1f482f926
# Parent  b21ba95c686bbfc5b69f34ef2760e979d30075ce
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@xxxxxxx>
Signed-off-by: Keir Fraser <keir@xxxxxxxxxxxxx>
---
 tools/libxc/xc_dom_core.c |    4 -
 tools/libxc/xc_private.c  |   97 +++++++++++++++++++++++++++++++++++++---------
 tools/libxc/xg_private.h  |    1 
 3 files changed, 82 insertions(+), 20 deletions(-)

diff -r b21ba95c686b -r 177ebf350b4c tools/libxc/xc_dom_core.c
--- a/tools/libxc/xc_dom_core.c Wed Sep 19 15:20:56 2007 +0100
+++ b/tools/libxc/xc_dom_core.c Wed Sep 19 15:42:56 2007 +0100
@@ -122,7 +122,7 @@ void *xc_dom_malloc_page_aligned(struct 
     memset(block, 0, sizeof(*block));
     block->mmap_len = size;
     block->mmap_ptr = mmap(NULL, block->mmap_len,
-                           PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
+                           PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON,
                            -1, 0);
     if ( block->mmap_ptr == MAP_FAILED )
     {
@@ -354,7 +354,7 @@ void *xc_dom_pfn_to_ptr(struct xc_dom_im
     {
         mode = "anonymous memory";
         phys->ptr = mmap(NULL, phys->count << page_shift,
-                         PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
+                         PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON,
                          -1, 0);
         if ( phys->ptr == MAP_FAILED )
         {
diff -r b21ba95c686b -r 177ebf350b4c tools/libxc/xc_private.c
--- a/tools/libxc/xc_private.c  Wed Sep 19 15:20:56 2007 +0100
+++ b/tools/libxc/xc_private.c  Wed Sep 19 15:42:56 2007 +0100
@@ -10,7 +10,12 @@
 #include <stdarg.h>
 #include <pthread.h>
 
-static __thread xc_error last_error = { XC_ERROR_NONE, ""};
+static pthread_key_t last_error_pkey;
+static pthread_once_t last_error_pkey_once = PTHREAD_ONCE_INIT;
+
+static pthread_key_t errbuf_pkey;
+static pthread_once_t errbuf_pkey_once = PTHREAD_ONCE_INIT;
+
 #if DEBUG
 static xc_error_handler error_handler = xc_default_error_handler;
 #else
@@ -23,15 +28,45 @@ void xc_default_error_handler(const xc_e
     fprintf(stderr, "ERROR %s: %s\n", desc, err->message);
 }
 
+static void
+_xc_clean_last_error(void *m)
+{
+    free(m);
+    pthread_setspecific(last_error_pkey, NULL);
+}
+
+static void
+_xc_init_last_error(void)
+{
+    pthread_key_create(&last_error_pkey, _xc_clean_last_error);
+}
+
+static xc_error *
+_xc_get_last_error(void)
+{
+    xc_error *last_error;
+
+    pthread_once(&last_error_pkey_once, _xc_init_last_error);
+
+    last_error = pthread_getspecific(last_error_pkey);
+    if (last_error == NULL) {
+        last_error = malloc(sizeof(xc_error));
+        pthread_setspecific(last_error_pkey, last_error);
+    }
+
+    return last_error;
+}
+
 const xc_error *xc_get_last_error(void)
 {
-    return &last_error;
+    return _xc_get_last_error();
 }
 
 void xc_clear_last_error(void)
 {
-    last_error.code = XC_ERROR_NONE;
-    last_error.message[0] = '\0';
+    xc_error *last_error = _xc_get_last_error();
+    last_error->code = XC_ERROR_NONE;
+    last_error->message[0] = '\0';
 }
 
 const char *xc_error_code_to_desc(int code)
@@ -61,12 +96,12 @@ xc_error_handler xc_set_error_handler(xc
     return old;
 }
 
-
 static void _xc_set_error(int code, const char *msg)
 {
-    last_error.code = code;
-    strncpy(last_error.message, msg, XC_MAX_ERROR_MSG_LEN - 1);
-    last_error.message[XC_MAX_ERROR_MSG_LEN-1] = '\0';
+    xc_error *last_error = _xc_get_last_error();
+    last_error->code = code;
+    strncpy(last_error->message, msg, XC_MAX_ERROR_MSG_LEN - 1);
+    last_error->message[XC_MAX_ERROR_MSG_LEN-1] = '\0';
 }
 
 void xc_set_error(int code, const char *fmt, ...)
@@ -84,23 +119,29 @@ void xc_set_error(int code, const char *
 
     errno = saved_errno;
 
-    if ( error_handler != NULL )
-        error_handler(&last_error);
+    if ( error_handler != NULL ) {
+        xc_error *last_error = _xc_get_last_error();
+        error_handler(last_error);
+    }
 }
 
 int lock_pages(void *addr, size_t len)
 {
       int e = 0;
 #ifndef __sun__
-      e = mlock(addr, len);
-#endif
-      return (e);
+      void *laddr = (void *)((unsigned long)addr & PAGE_MASK);
+      size_t llen = (len + PAGE_SIZE - 1) & PAGE_MASK;
+      e = mlock(laddr, llen);
+#endif
+      return e;
 }
 
 void unlock_pages(void *addr, size_t len)
 {
 #ifndef __sun__
-    safe_munlock(addr, len);
+    void *laddr = (void *)((unsigned long)addr & PAGE_MASK);
+    size_t llen = (len + PAGE_SIZE - 1) & PAGE_MASK;
+    safe_munlock(laddr, llen);
 #endif
 }
 
@@ -466,11 +507,33 @@ unsigned long xc_make_page_below_4G(
     return new_mfn;
 }
 
+static void
+_xc_clean_errbuf(void * m)
+{
+    free(m);
+    pthread_setspecific(errbuf_pkey, NULL);
+}
+
+static void
+_xc_init_errbuf(void)
+{
+    pthread_key_create(&errbuf_pkey, _xc_clean_errbuf);
+}
+
 char *safe_strerror(int errcode)
 {
-    static __thread char errbuf[32];
+#define XS_BUFSIZE 32
+    char *errbuf;
     static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
     char *strerror_str;
+
+    pthread_once(&errbuf_pkey_once, _xc_init_errbuf);
+
+    errbuf = pthread_getspecific(errbuf_pkey);
+    if (errbuf == NULL) {
+        errbuf = malloc(XS_BUFSIZE);
+        pthread_setspecific(errbuf_pkey, errbuf);
+    }
 
     /*
      * Thread-unsafe strerror() is protected by a local mutex. We copy
@@ -478,8 +541,8 @@ char *safe_strerror(int errcode)
      */
     pthread_mutex_lock(&mutex);
     strerror_str = strerror(errcode);
-    strncpy(errbuf, strerror_str, sizeof(errbuf));
-    errbuf[sizeof(errbuf)-1] = '\0';
+    strncpy(errbuf, strerror_str, XS_BUFSIZE);
+    errbuf[XS_BUFSIZE-1] = '\0';
     pthread_mutex_unlock(&mutex);
 
     return errbuf;
diff -r b21ba95c686b -r 177ebf350b4c tools/libxc/xg_private.h
--- a/tools/libxc/xg_private.h  Wed Sep 19 15:20:56 2007 +0100
+++ b/tools/libxc/xg_private.h  Wed Sep 19 15:42:56 2007 +0100
@@ -15,7 +15,6 @@
 #include "xenguest.h"
 #include "xc_private.h"
 
-#include <xen/sys/privcmd.h>
 #include <xen/memory.h>
 #include <xen/elfnote.h>
 

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.