Upstream: Replace sys-queue.h with qemu-queue.h

Change-Id: I5c51f54a7fe2ea702420429bbf0c789ed6d8c534
diff --git a/Makefile.android b/Makefile.android
index 4dbc22a..0639bfd 100644
--- a/Makefile.android
+++ b/Makefile.android
@@ -134,7 +134,6 @@
 
 LOCAL_SRC_FILES := \
     tcg/tcg.c \
-    tcg/tcg-runtime.c \
 
 include $(BUILD_HOST_STATIC_LIBRARY)
 
diff --git a/acl.c b/acl.c
index f69db25..345a2b4 100644
--- a/acl.c
+++ b/acl.c
@@ -64,7 +64,7 @@
     acl->defaultDeny = 1;
 
     acl->nentries = 0;
-    TAILQ_INIT(&acl->entries);
+    QTAILQ_INIT(&acl->entries);
 
     acls = qemu_realloc(acls, sizeof(*acls) * (nacls +1));
     acls[nacls] = acl;
@@ -78,7 +78,7 @@
 {
     qemu_acl_entry *entry;
 
-    TAILQ_FOREACH(entry, &acl->entries, next) {
+    QTAILQ_FOREACH(entry, &acl->entries, next) {
 #ifdef HAVE_FNMATCH_H
         if (fnmatch(entry->match, party, 0) == 0)
             return entry->deny ? 0 : 1;
@@ -102,8 +102,8 @@
      * of "open access" while the user re-initializes the
      * access control list */
     acl->defaultDeny = 1;
-    TAILQ_FOREACH(entry, &acl->entries, next) {
-        TAILQ_REMOVE(&acl->entries, entry, next);
+    QTAILQ_FOREACH(entry, &acl->entries, next) {
+        QTAILQ_REMOVE(&acl->entries, entry, next);
         free(entry->match);
         free(entry);
     }
@@ -121,7 +121,7 @@
     entry->match = qemu_strdup(match);
     entry->deny = deny;
 
-    TAILQ_INSERT_TAIL(&acl->entries, entry, next);
+    QTAILQ_INSERT_TAIL(&acl->entries, entry, next);
     acl->nentries++;
 
     return acl->nentries;
@@ -147,10 +147,10 @@
     entry->match = qemu_strdup(match);
     entry->deny = deny;
 
-    TAILQ_FOREACH(tmp, &acl->entries, next) {
+    QTAILQ_FOREACH(tmp, &acl->entries, next) {
         i++;
         if (i == index) {
-            TAILQ_INSERT_BEFORE(tmp, entry, next);
+            QTAILQ_INSERT_BEFORE(tmp, entry, next);
             acl->nentries++;
             break;
         }
@@ -165,10 +165,10 @@
     qemu_acl_entry *entry;
     int i = 0;
 
-    TAILQ_FOREACH(entry, &acl->entries, next) {
+    QTAILQ_FOREACH(entry, &acl->entries, next) {
         i++;
         if (strcmp(entry->match, match) == 0) {
-            TAILQ_REMOVE(&acl->entries, entry, next);
+            QTAILQ_REMOVE(&acl->entries, entry, next);
             return i;
         }
     }
diff --git a/acl.h b/acl.h
index 62a5e56..0ef7804 100644
--- a/acl.h
+++ b/acl.h
@@ -25,7 +25,7 @@
 #ifndef __QEMU_ACL_H__
 #define __QEMU_ACL_H__
 
-#include "sys-queue.h"
+#include "qemu-queue.h"
 
 typedef struct qemu_acl_entry qemu_acl_entry;
 typedef struct qemu_acl qemu_acl;
@@ -34,13 +34,13 @@
     char *match;
     int deny;
 
-    TAILQ_ENTRY(qemu_acl_entry) next;
+    QTAILQ_ENTRY(qemu_acl_entry) next;
 };
 
 struct qemu_acl {
     char *aclname;
     unsigned int nentries;
-    TAILQ_HEAD(,qemu_acl_entry) entries;
+    QTAILQ_HEAD(,qemu_acl_entry) entries;
     int defaultDeny;
 };
 
diff --git a/aio-android.c b/aio-android.c
index a5be934..53f7a6d 100644
--- a/aio-android.c
+++ b/aio-android.c
@@ -13,14 +13,14 @@
 
 #include "qemu-common.h"
 #include "block.h"
-#include "sys-queue.h"
+#include "qemu-queue.h"
 #include "qemu_socket.h"
 #include "iolooper.h"
 
 typedef struct AioHandler AioHandler;
 
 /* The list of registered AIO handlers */
-static LIST_HEAD(, AioHandler) aio_handlers;
+static QLIST_HEAD(, AioHandler) aio_handlers;
 
 /* This is a simple lock used to protect the aio_handlers list.  Specifically,
  * it's used to ensure that no callbacks are removed while we're walking and
@@ -36,14 +36,14 @@
     AioFlushHandler *io_flush;
     int deleted;
     void *opaque;
-    LIST_ENTRY(AioHandler) node;
+    QLIST_ENTRY(AioHandler) node;
 };
 
 static AioHandler *find_aio_handler(int fd)
 {
     AioHandler *node;
 
-    LIST_FOREACH(node, &aio_handlers, node) {
+    QLIST_FOREACH(node, &aio_handlers, node) {
         if (node->fd == fd)
             if (!node->deleted)
                 return node;
@@ -73,7 +73,7 @@
                  * deleted because deleted nodes are only cleaned up after
                  * releasing the walking_handlers lock.
                  */
-                LIST_REMOVE(node, node);
+                QLIST_REMOVE(node, node);
                 qemu_free(node);
             }
         }
@@ -82,7 +82,7 @@
             /* Alloc and insert if it's not already there */
             node = qemu_mallocz(sizeof(AioHandler));
             node->fd = fd;
-            LIST_INSERT_HEAD(&aio_handlers, node, node);
+            QLIST_INSERT_HEAD(&aio_handlers, node, node);
         }
         /* Update handler with latest information */
         node->io_read = io_read;
@@ -110,7 +110,7 @@
 	 */
         qemu_aio_wait();
 
-        LIST_FOREACH(node, &aio_handlers, node) {
+        QLIST_FOREACH(node, &aio_handlers, node) {
             ret |= node->io_flush(node->opaque);
         }
     } while (ret > 0);
@@ -133,7 +133,7 @@
         walking_handlers = 1;
 
         /* fill fd sets */
-        LIST_FOREACH(node, &aio_handlers, node) {
+        QLIST_FOREACH(node, &aio_handlers, node) {
             /* If there aren't pending AIO operations, don't invoke callbacks.
              * Otherwise, if there are no AIO requests, qemu_aio_wait() would
              * wait indefinitely.
@@ -160,7 +160,7 @@
 
             /* we have to walk very carefully in case
              * qemu_aio_set_fd_handler is called while we're walking */
-            node = LIST_FIRST(&aio_handlers);
+            node = QLIST_FIRST(&aio_handlers);
             while (node) {
                 AioHandler *tmp;
 
@@ -176,10 +176,10 @@
                 }
 
                 tmp = node;
-                node = LIST_NEXT(node, node);
+                node = QLIST_NEXT(node, node);
 
                 if (tmp->deleted) {
-                    LIST_REMOVE(tmp, node);
+                    QLIST_REMOVE(tmp, node);
                     qemu_free(tmp);
                 }
             }
diff --git a/aio.c b/aio.c
index dc9b85d..2f3f8e7 100644
--- a/aio.c
+++ b/aio.c
@@ -13,13 +13,13 @@
 
 #include "qemu-common.h"
 #include "block.h"
-#include "sys-queue.h"
+#include "qemu-queue.h"
 #include "qemu_socket.h"
 
 typedef struct AioHandler AioHandler;
 
 /* The list of registered AIO handlers */
-static LIST_HEAD(, AioHandler) aio_handlers;
+static QLIST_HEAD(, AioHandler) aio_handlers;
 
 /* This is a simple lock used to protect the aio_handlers list.  Specifically,
  * it's used to ensure that no callbacks are removed while we're walking and
@@ -35,14 +35,14 @@
     AioFlushHandler *io_flush;
     int deleted;
     void *opaque;
-    LIST_ENTRY(AioHandler) node;
+    QLIST_ENTRY(AioHandler) node;
 };
 
 static AioHandler *find_aio_handler(int fd)
 {
     AioHandler *node;
 
-    LIST_FOREACH(node, &aio_handlers, node) {
+    QLIST_FOREACH(node, &aio_handlers, node) {
         if (node->fd == fd)
             if (!node->deleted)
                 return node;
@@ -72,7 +72,7 @@
                  * deleted because deleted nodes are only cleaned up after
                  * releasing the walking_handlers lock.
                  */
-                LIST_REMOVE(node, node);
+                QLIST_REMOVE(node, node);
                 qemu_free(node);
             }
         }
@@ -81,7 +81,7 @@
             /* Alloc and insert if it's not already there */
             node = qemu_mallocz(sizeof(AioHandler));
             node->fd = fd;
-            LIST_INSERT_HEAD(&aio_handlers, node, node);
+            QLIST_INSERT_HEAD(&aio_handlers, node, node);
         }
         /* Update handler with latest information */
         node->io_read = io_read;
@@ -109,7 +109,7 @@
 	 */
         qemu_aio_wait();
 
-        LIST_FOREACH(node, &aio_handlers, node) {
+        QLIST_FOREACH(node, &aio_handlers, node) {
             ret |= node->io_flush(node->opaque);
         }
     } while (ret > 0);
@@ -133,7 +133,7 @@
         FD_ZERO(&wrfds);
 
         /* fill fd sets */
-        LIST_FOREACH(node, &aio_handlers, node) {
+        QLIST_FOREACH(node, &aio_handlers, node) {
             /* If there aren't pending AIO operations, don't invoke callbacks.
              * Otherwise, if there are no AIO requests, qemu_aio_wait() would
              * wait indefinitely.
@@ -168,7 +168,7 @@
 
             /* we have to walk very carefully in case
              * qemu_aio_set_fd_handler is called while we're walking */
-            node = LIST_FIRST(&aio_handlers);
+            node = QLIST_FIRST(&aio_handlers);
             while (node) {
                 AioHandler *tmp;
 
@@ -184,10 +184,10 @@
                 }
 
                 tmp = node;
-                node = LIST_NEXT(node, node);
+                node = QLIST_NEXT(node, node);
 
                 if (tmp->deleted) {
-                    LIST_REMOVE(tmp, node);
+                    QLIST_REMOVE(tmp, node);
                     qemu_free(tmp);
                 }
             }
diff --git a/audio/audio.c b/audio/audio.c
index cd4f0da..2d77f14 100644
--- a/audio/audio.c
+++ b/audio/audio.c
@@ -876,8 +876,8 @@
             sw->rate = NULL;
         }
 
-        LIST_REMOVE (sw, entries);
-        LIST_REMOVE (sc, entries);
+        QLIST_REMOVE (sw, entries);
+        QLIST_REMOVE (sc, entries);
         qemu_free (sc);
         if (was_active) {
             /* We have removed soft voice from the capture:
@@ -921,8 +921,8 @@
             qemu_free (sw);
             return -1;
         }
-        LIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
-        LIST_INSERT_HEAD (&hw->cap_head, sc, entries);
+        QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
+        QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
 #ifdef DEBUG_CAPTURE
         asprintf (&sw->name, "for %p %d,%d,%d",
                   hw, sw->info.freq, sw->info.bits, sw->info.nchannels);
@@ -1946,9 +1946,9 @@
         return;
     }
 
-    LIST_INIT (&s->hw_head_out);
-    LIST_INIT (&s->hw_head_in);
-    LIST_INIT (&s->cap_head);
+    QLIST_INIT (&s->hw_head_out);
+    QLIST_INIT (&s->hw_head_in);
+    QLIST_INIT (&s->cap_head);
     atexit (audio_atexit);
 
     s->ts = qemu_new_timer (vm_clock, audio_timer, s);
@@ -2001,7 +2001,7 @@
 
     initialized = 1;
 
-    LIST_INIT (&s->card_head);
+    QLIST_INIT (&s->card_head);
     register_savevm ("audio", 0, 1, audio_save, audio_load, s);
     qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks);
 }
@@ -2011,12 +2011,12 @@
     audio_init ();
     card->name = qemu_strdup (name);
     memset (&card->entries, 0, sizeof (card->entries));
-    LIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
+    QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
 }
 
 void AUD_remove_card (QEMUSoundCard *card)
 {
-    LIST_REMOVE (card, entries);
+    QLIST_REMOVE (card, entries);
     qemu_free (card->name);
 }
 
@@ -2053,7 +2053,7 @@
 
     cap = audio_pcm_capture_find_specific (as);
     if (cap) {
-        LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
+        QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
         return cap;
     }
     else {
@@ -2068,8 +2068,8 @@
         }
 
         hw = &cap->hw;
-        LIST_INIT (&hw->sw_head);
-        LIST_INIT (&cap->cb_head);
+        QLIST_INIT (&hw->sw_head);
+        QLIST_INIT (&cap->cb_head);
 
         /* XXX find a more elegant way */
         hw->samples = 4096 * 4;
@@ -2097,8 +2097,8 @@
             [hw->info.swap_endianness]
             [audio_bits_to_index (hw->info.bits)];
 
-        LIST_INSERT_HEAD (&s->cap_head, cap, entries);
-        LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
+        QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
+        QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
 
         hw = NULL;
         while ((hw = audio_pcm_hw_find_any_out (hw))) {
@@ -2124,7 +2124,7 @@
     for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
         if (cb->opaque == cb_opaque) {
             cb->ops.destroy (cb_opaque);
-            LIST_REMOVE (cb, entries);
+            QLIST_REMOVE (cb, entries);
             qemu_free (cb);
 
             if (!cap->cb_head.lh_first) {
@@ -2141,12 +2141,12 @@
                         st_rate_stop (sw->rate);
                         sw->rate = NULL;
                     }
-                    LIST_REMOVE (sw, entries);
-                    LIST_REMOVE (sc, entries);
+                    QLIST_REMOVE (sw, entries);
+                    QLIST_REMOVE (sc, entries);
                     qemu_free (sc);
                     sw = sw1;
                 }
-                LIST_REMOVE (cap, entries);
+                QLIST_REMOVE (cap, entries);
                 qemu_free (cap);
             }
             return;
diff --git a/audio/audio.h b/audio/audio.h
index ee923e4..13fc7cc 100644
--- a/audio/audio.h
+++ b/audio/audio.h
@@ -24,11 +24,10 @@
 #ifndef QEMU_AUDIO_H
 #define QEMU_AUDIO_H
 
-#include "config.h"
-#include "qemu-common.h"
-#include "sys-queue.h"
+#include "config-host.h"
+#include "qemu-queue.h"
 
-typedef void (*audio_callback_fn_t) (void *opaque, int avail);
+typedef void (*audio_callback_fn) (void *opaque, int avail);
 
 typedef enum {
     AUD_FMT_U8,
@@ -71,7 +70,7 @@
 typedef struct CaptureState {
     void *opaque;
     struct capture_ops ops;
-    LIST_ENTRY (CaptureState) entries;
+    QLIST_ENTRY (CaptureState) entries;
 } CaptureState;
 
 typedef struct SWVoiceOut SWVoiceOut;
@@ -80,7 +79,7 @@
 
 typedef struct QEMUSoundCard {
     char *name;
-    LIST_ENTRY (QEMUSoundCard) entries;
+    QLIST_ENTRY (QEMUSoundCard) entries;
 } QEMUSoundCard;
 
 typedef struct QEMUAudioTimeStamp {
@@ -109,7 +108,7 @@
     SWVoiceOut *sw,
     const char *name,
     void *callback_opaque,
-    audio_callback_fn_t callback_fn,
+    audio_callback_fn callback_fn,
     struct audsettings *settings
     );
 
@@ -130,7 +129,7 @@
     SWVoiceIn *sw,
     const char *name,
     void *callback_opaque,
-    audio_callback_fn_t callback_fn,
+    audio_callback_fn callback_fn,
     struct audsettings *settings
     );
 
@@ -148,9 +147,6 @@
     return (d + incr);
 }
 
-uint32_t popcount (uint32_t u);
-uint32_t lsbindex (uint32_t u);
-
 #ifdef __GNUC__
 #define audio_MIN(a, b) ( __extension__ ({      \
     __typeof (a) ta = a;                        \
diff --git a/audio/audio_int.h b/audio/audio_int.h
index 0ae03fd..a78f394 100644
--- a/audio/audio_int.h
+++ b/audio/audio_int.h
@@ -52,7 +52,7 @@
 
 struct audio_callback {
     void *opaque;
-    audio_callback_fn_t fn;
+    audio_callback_fn fn;
 };
 
 struct audio_pcm_info {
@@ -81,10 +81,10 @@
     struct st_sample *mix_buf;
 
     int samples;
-    LIST_HEAD (sw_out_listhead, SWVoiceOut) sw_head;
-    LIST_HEAD (sw_cap_listhead, SWVoiceCap) cap_head;
+    QLIST_HEAD (sw_out_listhead, SWVoiceOut) sw_head;
+    QLIST_HEAD (sw_cap_listhead, SWVoiceCap) cap_head;
     struct audio_pcm_ops *pcm_ops;
-    LIST_ENTRY (HWVoiceOut) entries;
+    QLIST_ENTRY (HWVoiceOut) entries;
 } HWVoiceOut;
 
 typedef struct HWVoiceIn {
@@ -100,9 +100,9 @@
     struct st_sample *conv_buf;
 
     int samples;
-    LIST_HEAD (sw_in_listhead, SWVoiceIn) sw_head;
+    QLIST_HEAD (sw_in_listhead, SWVoiceIn) sw_head;
     struct audio_pcm_ops *pcm_ops;
-    LIST_ENTRY (HWVoiceIn) entries;
+    QLIST_ENTRY (HWVoiceIn) entries;
 } HWVoiceIn;
 
 struct SWVoiceOut {
@@ -119,7 +119,7 @@
     char *name;
     struct mixeng_volume vol;
     struct audio_callback callback;
-    LIST_ENTRY (SWVoiceOut) entries;
+    QLIST_ENTRY (SWVoiceOut) entries;
 };
 
 struct SWVoiceIn {
@@ -135,7 +135,7 @@
     char *name;
     struct mixeng_volume vol;
     struct audio_callback callback;
-    LIST_ENTRY (SWVoiceIn) entries;
+    QLIST_ENTRY (SWVoiceIn) entries;
 };
 
 struct audio_driver {
@@ -169,20 +169,20 @@
 struct capture_callback {
     struct audio_capture_ops ops;
     void *opaque;
-    LIST_ENTRY (capture_callback) entries;
+    QLIST_ENTRY (capture_callback) entries;
 };
 
 struct CaptureVoiceOut {
     HWVoiceOut hw;
     void *buf;
-    LIST_HEAD (cb_listhead, capture_callback) cb_head;
-    LIST_ENTRY (CaptureVoiceOut) entries;
+    QLIST_HEAD (cb_listhead, capture_callback) cb_head;
+    QLIST_ENTRY (CaptureVoiceOut) entries;
 };
 
 struct SWVoiceCap {
     SWVoiceOut sw;
     CaptureVoiceOut *cap;
-    LIST_ENTRY (SWVoiceCap) entries;
+    QLIST_ENTRY (SWVoiceCap) entries;
 };
 
 struct AudioState {
@@ -192,10 +192,10 @@
     void*                 drv_out_opaque;
 
     QEMUTimer *ts;
-    LIST_HEAD (card_listhead, QEMUSoundCard) card_head;
-    LIST_HEAD (hw_in_listhead, HWVoiceIn) hw_head_in;
-    LIST_HEAD (hw_out_listhead, HWVoiceOut) hw_head_out;
-    LIST_HEAD (cap_listhead, CaptureVoiceOut) cap_head;
+    QLIST_HEAD (card_listhead, QEMUSoundCard) card_head;
+    QLIST_HEAD (hw_in_listhead, HWVoiceIn) hw_head_in;
+    QLIST_HEAD (hw_out_listhead, HWVoiceOut) hw_head_out;
+    QLIST_HEAD (cap_listhead, CaptureVoiceOut) cap_head;
     int nb_hw_voices_out;
     int nb_hw_voices_in;
     int vm_running;
diff --git a/audio/audio_pt_int.c b/audio/audio_pt_int.c
index c753774..e889a98 100644
--- a/audio/audio_pt_int.c
+++ b/audio/audio_pt_int.c
@@ -1,3 +1,4 @@
+#include "qemu-common.h"
 #include "audio.h"
 
 #define AUDIO_CAP "audio-pt"
diff --git a/audio/audio_template.h b/audio/audio_template.h
index d6c1037..9f75f19 100644
--- a/audio/audio_template.h
+++ b/audio/audio_template.h
@@ -184,12 +184,12 @@
 
 static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw)
 {
-    LIST_INSERT_HEAD (&hw->sw_head, sw, entries);
+    QLIST_INSERT_HEAD (&hw->sw_head, sw, entries);
 }
 
 static void glue (audio_pcm_hw_del_sw_, TYPE) (SW *sw)
 {
-    LIST_REMOVE (sw, entries);
+    QLIST_REMOVE (sw, entries);
 }
 
 static void glue (audio_pcm_hw_gc_, TYPE) (HW **hwp)
@@ -201,7 +201,7 @@
 #ifdef DAC
         audio_detach_capture (hw);
 #endif
-        LIST_REMOVE (hw, entries);
+        QLIST_REMOVE (hw, entries);
         glue (s->nb_hw_voices_, TYPE) += 1;
         glue (audio_pcm_hw_free_resources_ ,TYPE) (hw);
         BEGIN_NOSIGALRM
@@ -270,9 +270,9 @@
     }
 
     hw->pcm_ops = drv->pcm_ops;
-    LIST_INIT (&hw->sw_head);
+    QLIST_INIT (&hw->sw_head);
 #ifdef DAC
-    LIST_INIT (&hw->cap_head);
+    QLIST_INIT (&hw->cap_head);
 #endif
     BEGIN_NOSIGALRM
     err = glue (hw->pcm_ops->init_, TYPE) (hw, as);
@@ -299,7 +299,7 @@
         goto err1;
     }
 
-    LIST_INSERT_HEAD (&s->glue (hw_head_, TYPE), hw, entries);
+    QLIST_INSERT_HEAD (&s->glue (hw_head_, TYPE), hw, entries);
     glue (s->nb_hw_voices_, TYPE) -= 1;
 #ifdef DAC
     audio_attach_capture (hw);
@@ -409,7 +409,7 @@
     SW *sw,
     const char *name,
     void *callback_opaque ,
-    audio_callback_fn_t callback_fn,
+    audio_callback_fn callback_fn,
     struct audsettings *as
     )
 {
@@ -492,32 +492,30 @@
         }
     }
 
-    if (sw) {
-        sw->card = card;
-        sw->vol = nominal_volume;
-        sw->callback.fn = callback_fn;
-        sw->callback.opaque = callback_opaque;
+    sw->card = card;
+    sw->vol = nominal_volume;
+    sw->callback.fn = callback_fn;
+    sw->callback.opaque = callback_opaque;
 
 #ifdef DAC
-        if (live) {
-            int mixed =
-                (live << old_sw->info.shift)
-                * old_sw->info.bytes_per_second
-                / sw->info.bytes_per_second;
+    if (live) {
+        int mixed =
+            (live << old_sw->info.shift)
+            * old_sw->info.bytes_per_second
+            / sw->info.bytes_per_second;
 
 #ifdef DEBUG_PLIVE
-            dolog ("Silence will be mixed %d\n", mixed);
+        dolog ("Silence will be mixed %d\n", mixed);
 #endif
-            sw->total_hw_samples_mixed += mixed;
-        }
+        sw->total_hw_samples_mixed += mixed;
+    }
 #endif
 
 #ifdef DEBUG_AUDIO
-        dolog ("%s\n", name);
-        audio_pcm_print_info ("hw", &sw->hw->info);
-        audio_pcm_print_info ("sw", &sw->info);
+    dolog ("%s\n", name);
+    audio_pcm_print_info ("hw", &sw->hw->info);
+    audio_pcm_print_info ("sw", &sw->info);
 #endif
-    }
 
     return sw;
 
@@ -563,7 +561,7 @@
         return 0;
     }
 
-    return (delta * sw->hw->info.freq) / 1000000;
+    return muldiv64 (delta, sw->hw->info.freq, 1000000);
 }
 
 #undef TYPE
diff --git a/block.c b/block.c
index 881bd2d..6d05a58 100644
--- a/block.c
+++ b/block.c
@@ -23,7 +23,7 @@
  */
 #include "config-host.h"
 #ifdef CONFIG_BSD
-/* include native header before sys-queue.h */
+/* include native header before qemu-queue.h */
 #include <sys/queue.h>
 #endif
 
diff --git a/cpu-defs.h b/cpu-defs.h
index 9518d47..2907f45 100644
--- a/cpu-defs.h
+++ b/cpu-defs.h
@@ -31,7 +31,7 @@
 #include <inttypes.h>
 #include <signal.h>
 #include "osdep.h"
-#include "sys-queue.h"
+#include "qemu-queue.h"
 #include "targphys.h"
 
 #ifndef TARGET_LONG_BITS
@@ -127,14 +127,14 @@
 typedef struct CPUBreakpoint {
     target_ulong pc;
     int flags; /* BP_* */
-    TAILQ_ENTRY(CPUBreakpoint) entry;
+    QTAILQ_ENTRY(CPUBreakpoint) entry;
 } CPUBreakpoint;
 
 typedef struct CPUWatchpoint {
     target_ulong vaddr;
     target_ulong len_mask;
     int flags; /* BP_* */
-    TAILQ_ENTRY(CPUWatchpoint) entry;
+    QTAILQ_ENTRY(CPUWatchpoint) entry;
 } CPUWatchpoint;
 
 #define CPU_TEMP_BUF_NLONGS 128
@@ -172,10 +172,10 @@
                                                                         \
     /* from this point: preserved by CPU reset */                       \
     /* ice debug support */                                             \
-    TAILQ_HEAD(breakpoints_head, CPUBreakpoint) breakpoints;            \
+    QTAILQ_HEAD(breakpoints_head, CPUBreakpoint) breakpoints;            \
     int singlestep_enabled;                                             \
                                                                         \
-    TAILQ_HEAD(watchpoints_head, CPUWatchpoint) watchpoints;            \
+    QTAILQ_HEAD(watchpoints_head, CPUWatchpoint) watchpoints;            \
     CPUWatchpoint *watchpoint_hit;                                      \
                                                                         \
     struct GDBRegisterState *gdb_regs;                                  \
@@ -188,6 +188,8 @@
     int cpu_index; /* CPU index (informative) */                        \
     uint32_t host_tid; /* host thread ID */                             \
     int numa_node; /* NUMA node this cpu is belonging to  */            \
+    int nr_cores;  /* number of cores within this CPU package */        \
+    int nr_threads;/* number of threads within this CPU */              \
     int running; /* Nonzero if cpu is currently running(usermode).  */  \
     /* user data */                                                     \
     void *opaque;                                                       \
diff --git a/cpu-exec.c b/cpu-exec.c
index 7bc432d..adbea80 100644
--- a/cpu-exec.c
+++ b/cpu-exec.c
@@ -14,8 +14,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 #include "config.h"
 #include "exec.h"
@@ -47,7 +46,7 @@
 
 int tb_invalidated_flag;
 
-//#define DEBUG_EXEC
+//#define CONFIG_DEBUG_EXEC
 //#define DEBUG_SIGNAL
 
 int qemu_cpu_has_work(CPUState *env)
@@ -203,7 +202,7 @@
     CPUWatchpoint *wp;
 
     if (!env->watchpoint_hit)
-        TAILQ_FOREACH(wp, &env->watchpoints, entry)
+        QTAILQ_FOREACH(wp, &env->watchpoints, entry)
             wp->flags &= ~BP_WATCHPOINT_HIT;
 
     if (debug_excp_handler)
@@ -250,6 +249,7 @@
 #elif defined(TARGET_MIPS)
 #elif defined(TARGET_SH4)
 #elif defined(TARGET_CRIS)
+#elif defined(TARGET_S390X)
     /* XXXXX */
 #else
 #error unsupported target CPU
@@ -318,35 +318,10 @@
 #elif defined(TARGET_M68K)
                     do_interrupt(0);
 #endif
+                    env->exception_index = -1;
 #endif
                 }
-                env->exception_index = -1;
             }
-#ifdef CONFIG_KQEMU
-            if (kqemu_is_ok(env) && env->interrupt_request == 0 && env->exit_request == 0) {
-                int ret;
-                env->eflags = env->eflags | helper_cc_compute_all(CC_OP) | (DF & DF_MASK);
-                ret = kqemu_cpu_exec(env);
-                /* put eflags in CPU temporary format */
-                CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
-                DF = 1 - (2 * ((env->eflags >> 10) & 1));
-                CC_OP = CC_OP_EFLAGS;
-                env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
-                if (ret == 1) {
-                    /* exception */
-                    longjmp(env->jmp_env, 1);
-                } else if (ret == 2) {
-                    /* softmmu execution needed */
-                } else {
-                    if (env->interrupt_request != 0 || env->exit_request != 0) {
-                        /* hardware interrupt will be executed just after */
-                    } else {
-                        /* otherwise, we restart */
-                        longjmp(env->jmp_env, 1);
-                    }
-                }
-            }
-#endif
 
             if (kvm_enabled()) {
                 kvm_cpu_exec(env);
@@ -380,7 +355,14 @@
                     }
 #endif
 #if defined(TARGET_I386)
-                    if (env->hflags2 & HF2_GIF_MASK) {
+                    if (interrupt_request & CPU_INTERRUPT_INIT) {
+                            svm_check_intercept(SVM_EXIT_INIT);
+                            do_cpu_init(env);
+                            env->exception_index = EXCP_HALTED;
+                            cpu_loop_exit();
+                    } else if (interrupt_request & CPU_INTERRUPT_SIPI) {
+                            do_cpu_sipi(env);
+                    } else if (env->hflags2 & HF2_GIF_MASK) {
                         if ((interrupt_request & CPU_INTERRUPT_SMI) &&
                             !(env->hflags & HF_SMM_MASK)) {
                             svm_check_intercept(SVM_EXIT_SMI);
@@ -393,6 +375,10 @@
                             env->hflags2 |= HF2_NMI_MASK;
                             do_interrupt(EXCP02_NMI, 0, 0, 0, 1);
                             next_tb = 0;
+			} else if (interrupt_request & CPU_INTERRUPT_MCE) {
+                            env->interrupt_request &= ~CPU_INTERRUPT_MCE;
+                            do_interrupt(EXCP12_MCHK, 0, 0, 0, 0);
+                            next_tb = 0;
                         } else if ((interrupt_request & CPU_INTERRUPT_HARD) &&
                                    (((env->hflags2 & HF2_VINTR_MASK) && 
                                      (env->hflags2 & HF2_HIF_MASK)) ||
@@ -431,7 +417,7 @@
 #elif defined(TARGET_PPC)
 #if 0
                     if ((interrupt_request & CPU_INTERRUPT_RESET)) {
-                        cpu_ppc_reset(env);
+                        cpu_reset(env);
                     }
 #endif
                     if (interrupt_request & CPU_INTERRUPT_HARD) {
diff --git a/envlist.c b/envlist.c
new file mode 100644
index 0000000..f2303cd
--- /dev/null
+++ b/envlist.c
@@ -0,0 +1,246 @@
+#include <assert.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "qemu-queue.h"
+#include "envlist.h"
+
+struct envlist_entry {
+	const char *ev_var;			/* actual env value */
+	QLIST_ENTRY(envlist_entry) ev_link;
+};
+
+struct envlist {
+	QLIST_HEAD(, envlist_entry) el_entries;	/* actual entries */
+	size_t el_count;			/* number of entries */
+};
+
+static int envlist_parse(envlist_t *envlist,
+    const char *env, int (*)(envlist_t *, const char *));
+
+/*
+ * Allocates new envlist and returns pointer to that or
+ * NULL in case of error.
+ */
+envlist_t *
+envlist_create(void)
+{
+	envlist_t *envlist;
+
+	if ((envlist = malloc(sizeof (*envlist))) == NULL)
+		return (NULL);
+
+	QLIST_INIT(&envlist->el_entries);
+	envlist->el_count = 0;
+
+	return (envlist);
+}
+
+/*
+ * Releases given envlist and its entries.
+ */
+void
+envlist_free(envlist_t *envlist)
+{
+	struct envlist_entry *entry;
+
+	assert(envlist != NULL);
+
+	while (envlist->el_entries.lh_first != NULL) {
+		entry = envlist->el_entries.lh_first;
+		QLIST_REMOVE(entry, ev_link);
+
+		free((char *)entry->ev_var);
+		free(entry);
+	}
+	free(envlist);
+}
+
+/*
+ * Parses comma separated list of set/modify environment
+ * variable entries and updates given enlist accordingly.
+ *
+ * For example:
+ *     envlist_parse(el, "HOME=foo,SHELL=/bin/sh");
+ *
+ * inserts/sets environment variables HOME and SHELL.
+ *
+ * Returns 0 on success, errno otherwise.
+ */
+int
+envlist_parse_set(envlist_t *envlist, const char *env)
+{
+	return (envlist_parse(envlist, env, &envlist_setenv));
+}
+
+/*
+ * Parses comma separated list of unset environment variable
+ * entries and removes given variables from given envlist.
+ *
+ * Returns 0 on success, errno otherwise.
+ */
+int
+envlist_parse_unset(envlist_t *envlist, const char *env)
+{
+	return (envlist_parse(envlist, env, &envlist_unsetenv));
+}
+
+/*
+ * Parses comma separated list of set, modify or unset entries
+ * and calls given callback for each entry.
+ *
+ * Returns 0 in case of success, errno otherwise.
+ */
+static int
+envlist_parse(envlist_t *envlist, const char *env,
+    int (*callback)(envlist_t *, const char *))
+{
+	char *tmpenv, *envvar;
+	char *envsave = NULL;
+
+	assert(callback != NULL);
+
+	if ((envlist == NULL) || (env == NULL))
+		return (EINVAL);
+
+	/*
+	 * We need to make temporary copy of the env string
+	 * as strtok_r(3) modifies it while it tokenizes.
+	 */
+	if ((tmpenv = strdup(env)) == NULL)
+		return (errno);
+
+	envvar = strtok_r(tmpenv, ",", &envsave);
+	while (envvar != NULL) {
+		if ((*callback)(envlist, envvar) != 0) {
+			free(tmpenv);
+			return (errno);
+		}
+		envvar = strtok_r(NULL, ",", &envsave);
+	}
+
+	free(tmpenv);
+	return (0);
+}
+
+/*
+ * Sets environment value to envlist in similar manner
+ * than putenv(3).
+ *
+ * Returns 0 in success, errno otherwise.
+ */
+int
+envlist_setenv(envlist_t *envlist, const char *env)
+{
+	struct envlist_entry *entry = NULL;
+	const char *eq_sign;
+	size_t envname_len;
+
+	if ((envlist == NULL) || (env == NULL))
+		return (EINVAL);
+
+	/* find out first equals sign in given env */
+	if ((eq_sign = strchr(env, '=')) == NULL)
+		return (EINVAL);
+	envname_len = eq_sign - env + 1;
+
+	/*
+	 * If there already exists variable with given name
+	 * we remove and release it before allocating a whole
+	 * new entry.
+	 */
+	for (entry = envlist->el_entries.lh_first; entry != NULL;
+	    entry = entry->ev_link.le_next) {
+		if (strncmp(entry->ev_var, env, envname_len) == 0)
+			break;
+	}
+
+	if (entry != NULL) {
+		QLIST_REMOVE(entry, ev_link);
+		free((char *)entry->ev_var);
+		free(entry);
+	} else {
+		envlist->el_count++;
+	}
+
+	if ((entry = malloc(sizeof (*entry))) == NULL)
+		return (errno);
+	if ((entry->ev_var = strdup(env)) == NULL) {
+		free(entry);
+		return (errno);
+	}
+	QLIST_INSERT_HEAD(&envlist->el_entries, entry, ev_link);
+
+	return (0);
+}
+
+/*
+ * Removes given env value from envlist in similar manner
+ * than unsetenv(3).  Returns 0 in success, errno otherwise.
+ */
+int
+envlist_unsetenv(envlist_t *envlist, const char *env)
+{
+	struct envlist_entry *entry;
+	size_t envname_len;
+
+	if ((envlist == NULL) || (env == NULL))
+		return (EINVAL);
+
+	/* env is not allowed to contain '=' */
+	if (strchr(env, '=') != NULL)
+		return (EINVAL);
+
+	/*
+	 * Find out the requested entry and remove
+	 * it from the list.
+	 */
+	envname_len = strlen(env);
+	for (entry = envlist->el_entries.lh_first; entry != NULL;
+	    entry = entry->ev_link.le_next) {
+		if (strncmp(entry->ev_var, env, envname_len) == 0)
+			break;
+	}
+	if (entry != NULL) {
+		QLIST_REMOVE(entry, ev_link);
+		free((char *)entry->ev_var);
+		free(entry);
+
+		envlist->el_count--;
+	}
+	return (0);
+}
+
+/*
+ * Returns given envlist as array of strings (in same form that
+ * global variable environ is).  Caller must free returned memory
+ * by calling free(3) for each element and for the array.  Returned
+ * array and given envlist are not related (no common references).
+ *
+ * If caller provides count pointer, number of items in array is
+ * stored there.  In case of error, NULL is returned and no memory
+ * is allocated.
+ */
+char **
+envlist_to_environ(const envlist_t *envlist, size_t *count)
+{
+	struct envlist_entry *entry;
+	char **env, **penv;
+
+	penv = env = malloc((envlist->el_count + 1) * sizeof (char *));
+	if (env == NULL)
+		return (NULL);
+
+	for (entry = envlist->el_entries.lh_first; entry != NULL;
+	    entry = entry->ev_link.le_next) {
+		*(penv++) = strdup(entry->ev_var);
+	}
+	*penv = NULL; /* NULL terminate the list */
+
+	if (count != NULL)
+		*count = envlist->el_count;
+
+	return (env);
+}
diff --git a/envlist.h b/envlist.h
new file mode 100644
index 0000000..e76d4a1
--- /dev/null
+++ b/envlist.h
@@ -0,0 +1,22 @@
+#ifndef ENVLIST_H
+#define ENVLIST_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct envlist envlist_t;
+
+extern	envlist_t *envlist_create(void);
+extern	void envlist_free(envlist_t *);
+extern	int envlist_setenv(envlist_t *, const char *);
+extern	int envlist_unsetenv(envlist_t *, const char *);
+extern	int envlist_parse_set(envlist_t *, const char *);
+extern	int envlist_parse_unset(envlist_t *, const char *);
+extern	char **envlist_to_environ(const envlist_t *, size_t *);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ENVLIST_H */
diff --git a/exec.c b/exec.c
index ce6aeda..f8cb9d6 100644
--- a/exec.c
+++ b/exec.c
@@ -14,8 +14,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 #include "config.h"
 #ifdef _WIN32
@@ -75,12 +74,11 @@
 #define TARGET_VIRT_ADDR_SPACE_BITS 42
 #elif defined(TARGET_PPC64)
 #define TARGET_PHYS_ADDR_SPACE_BITS 42
-#elif defined(TARGET_X86_64) && !defined(CONFIG_KQEMU)
+#elif defined(TARGET_X86_64)
 #define TARGET_PHYS_ADDR_SPACE_BITS 42
-#elif defined(TARGET_I386) && !defined(CONFIG_KQEMU)
+#elif defined(TARGET_I386)
 #define TARGET_PHYS_ADDR_SPACE_BITS 36
 #else
-/* Note: for compatibility with kqemu, we use 32 bits for x86_64 */
 #define TARGET_PHYS_ADDR_SPACE_BITS 32
 #endif
 
@@ -98,6 +96,10 @@
 #define code_gen_section                                \
     __attribute__((__section__(".gen_code")))           \
     __attribute__((aligned (32)))
+#elif defined(_WIN32)
+/* Maximum alignment for Win32 is 16. */
+#define code_gen_section                                \
+    __attribute__((aligned (16)))
 #else
 #define code_gen_section                                \
     __attribute__((aligned (32)))
@@ -194,7 +196,11 @@
 #endif
 
 /* log support */
+#ifdef WIN32
+static const char *logfilename = "qemu.log";
+#else
 static const char *logfilename = "/tmp/qemu.log";
+#endif
 FILE *logfile;
 int loglevel;
 static int log_append = 0;
@@ -317,7 +323,7 @@
 #if defined(CONFIG_USER_ONLY)
         size_t len = sizeof(PageDesc) * L2_SIZE;
         /* Don't use qemu_malloc because it may recurse.  */
-        p = mmap(0, len, PROT_READ | PROT_WRITE,
+        p = mmap(NULL, len, PROT_READ | PROT_WRITE,
                  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
         *lp = p;
         if (h2g_valid(p)) {
@@ -342,8 +348,9 @@
         return NULL;
 
     p = *lp;
-    if (!p)
-        return 0;
+    if (!p) {
+        return NULL;
+    }
     return p + (index & (L2_SIZE - 1));
 }
 
@@ -464,7 +471,7 @@
             exit(1);
         }
     }
-#elif defined(__FreeBSD__) || defined(__DragonFly__)
+#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
     {
         int flags;
         void *addr = NULL;
@@ -575,8 +582,8 @@
     }
     env->cpu_index = cpu_index;
     env->numa_node = 0;
-    TAILQ_INIT(&env->breakpoints);
-    TAILQ_INIT(&env->watchpoints);
+    QTAILQ_INIT(&env->breakpoints);
+    QTAILQ_INIT(&env->watchpoints);
     *penv = env;
 #if defined(CONFIG_USER_ONLY)
     cpu_list_unlock();
@@ -667,7 +674,8 @@
         for(tb = tb_phys_hash[i]; tb != NULL; tb = tb->phys_hash_next) {
             if (!(address + TARGET_PAGE_SIZE <= tb->pc ||
                   address >= tb->pc + tb->size)) {
-                printf("ERROR invalidate: address=%08lx PC=%08lx size=%04x\n",
+                printf("ERROR invalidate: address=" TARGET_FMT_lx
+                       " PC=%08lx size=%04x\n",
                        address, (long)tb->pc, tb->size);
             }
         }
@@ -692,26 +700,6 @@
     }
 }
 
-static void tb_jmp_check(TranslationBlock *tb)
-{
-    TranslationBlock *tb1;
-    unsigned int n1;
-
-    /* suppress any remaining jumps to this TB */
-    tb1 = tb->jmp_first;
-    for(;;) {
-        n1 = (long)tb1 & 3;
-        tb1 = (TranslationBlock *)((long)tb1 & ~3);
-        if (n1 == 2)
-            break;
-        tb1 = tb1->jmp_next[n1];
-    }
-    /* check end of list */
-    if (tb1 != tb) {
-        printf("ERROR: jmp_list from 0x%08lx\n", (long)tb);
-    }
-}
-
 #endif
 
 /* invalidate one TB */
@@ -1384,9 +1372,9 @@
 
     /* keep all GDB-injected watchpoints in front */
     if (flags & BP_GDB)
-        TAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
+        QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry);
     else
-        TAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
+        QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry);
 
     tlb_flush_page(env, addr);
 
@@ -1402,7 +1390,7 @@
     target_ulong len_mask = ~(len - 1);
     CPUWatchpoint *wp;
 
-    TAILQ_FOREACH(wp, &env->watchpoints, entry) {
+    QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
         if (addr == wp->vaddr && len_mask == wp->len_mask
                 && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
             cpu_watchpoint_remove_by_ref(env, wp);
@@ -1415,7 +1403,7 @@
 /* Remove a specific watchpoint by reference.  */
 void cpu_watchpoint_remove_by_ref(CPUState *env, CPUWatchpoint *watchpoint)
 {
-    TAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
+    QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry);
 
     tlb_flush_page(env, watchpoint->vaddr);
 
@@ -1427,7 +1415,7 @@
 {
     CPUWatchpoint *wp, *next;
 
-    TAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
+    QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) {
         if (wp->flags & mask)
             cpu_watchpoint_remove_by_ref(env, wp);
     }
@@ -1447,9 +1435,9 @@
 
     /* keep all GDB-injected breakpoints in front */
     if (flags & BP_GDB)
-        TAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
+        QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry);
     else
-        TAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
+        QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry);
 
     breakpoint_invalidate(env, pc);
 
@@ -1467,7 +1455,7 @@
 #if defined(TARGET_HAS_ICE)
     CPUBreakpoint *bp;
 
-    TAILQ_FOREACH(bp, &env->breakpoints, entry) {
+    QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
         if (bp->pc == pc && bp->flags == flags) {
             cpu_breakpoint_remove_by_ref(env, bp);
             return 0;
@@ -1483,7 +1471,7 @@
 void cpu_breakpoint_remove_by_ref(CPUState *env, CPUBreakpoint *breakpoint)
 {
 #if defined(TARGET_HAS_ICE)
-    TAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
+    QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry);
 
     breakpoint_invalidate(env, breakpoint->pc);
 
@@ -1497,7 +1485,7 @@
 #if defined(TARGET_HAS_ICE)
     CPUBreakpoint *bp, *next;
 
-    TAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
+    QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) {
         if (bp->flags & mask)
             cpu_breakpoint_remove_by_ref(env, bp);
     }
@@ -1538,7 +1526,8 @@
             static char logfile_buf[4096];
             setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
         }
-#else
+#elif !defined(_WIN32)
+        /* Win32 doesn't support line-buffering and requires size >= 2 */
         setvbuf(logfile, NULL, _IOLBF, 0);
 #endif
         log_append = 1;
@@ -1561,12 +1550,10 @@
 
 static void cpu_unlink_tb(CPUState *env)
 {
-#if defined(USE_NPTL)
     /* FIXME: TB unchaining isn't SMP safe.  For now just ignore the
        problem and hope the cpu will stop of its own accord.  For userspace
        emulation this often isn't actually as bad as it sounds.  Often
        signals are used primarily to interrupt blocking syscalls.  */
-#else
     TranslationBlock *tb;
     static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
 
@@ -1578,7 +1565,6 @@
         tb_reset_jump_recursive(tb);
         resetlock(&interrupt_lock);
     }
-#endif
 }
 
 /* mask must never be zero, except for A20 change call */
@@ -1747,13 +1733,13 @@
     /* Clone all break/watchpoints.
        Note: Once we support ptrace with hw-debug register access, make sure
        BP_CPU break/watchpoints are handled correctly on clone. */
-    TAILQ_INIT(&env->breakpoints);
-    TAILQ_INIT(&env->watchpoints);
+    QTAILQ_INIT(&env->breakpoints);
+    QTAILQ_INIT(&env->watchpoints);
 #if defined(TARGET_HAS_ICE)
-    TAILQ_FOREACH(bp, &env->breakpoints, entry) {
+    QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
         cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL);
     }
-    TAILQ_FOREACH(wp, &env->watchpoints, entry) {
+    QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
         cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1,
                               wp->flags, NULL);
     }
@@ -1843,12 +1829,6 @@
         tlb_flush_entry(&env->tlb_table[mmu_idx][i], addr);
 
     tlb_flush_jmp_cache(env, addr);
-
-#ifdef CONFIG_KQEMU
-    if (env->kqemu_enabled) {
-        kqemu_flush_page(env, addr);
-    }
-#endif
 }
 
 /* update the TLBs so that writes to code in the virtual page 'addr'
@@ -1896,18 +1876,6 @@
     if (length == 0)
         return;
     len = length >> TARGET_PAGE_BITS;
-#ifdef CONFIG_KQEMU
-    /* XXX: should not depend on cpu context */
-    env = first_cpu;
-    if (env->kqemu_enabled) {
-        ram_addr_t addr;
-        addr = start;
-        for(i = 0; i < len; i++) {
-            kqemu_set_notdirty(env, addr);
-            addr += TARGET_PAGE_SIZE;
-        }
-    }
-#endif
     mask = ~dirty_flags;
     p = phys_ram_dirty + (start >> TARGET_PAGE_BITS);
     for(i = 0; i < len; i++)
@@ -2064,7 +2032,7 @@
     code_address = address;
     /* Make accesses to pages with watchpoints go via the
        watchpoint trap routines.  */
-    TAILQ_FOREACH(wp, &env->watchpoints, entry) {
+    QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
         if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) {
             iotlb = io_mem_watch + paddr;
             /* TODO: The memory case can be optimized by not trapping
@@ -2377,8 +2345,9 @@
         }                                                               \
     } while (0)
 
-/* register physical memory. 'size' must be a multiple of the target
-   page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
+/* register physical memory.
+   For RAM, 'size' must be a multiple of the target page size.
+   If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
    io memory page.  The address used when calling the IO function is
    the offset from the start of the region, plus region_offset.  Both
    start_addr and region_offset are rounded down to a page boundary
@@ -2395,13 +2364,6 @@
     ram_addr_t orig_size = size;
     void *subpage;
 
-#ifdef CONFIG_KQEMU
-    /* XXX: should not depend on cpu context */
-    env = first_cpu;
-    if (env->kqemu_enabled) {
-        kqemu_set_phys_mem(start_addr, size, phys_offset);
-    }
-#endif
     if (kvm_enabled())
         kvm_set_phys_mem(start_addr, size, phys_offset);
 
@@ -2496,36 +2458,23 @@
         kvm_uncoalesce_mmio_region(addr, size);
 }
 
-#ifdef CONFIG_KQEMU
-/* XXX: better than nothing */
-static ram_addr_t kqemu_ram_alloc(ram_addr_t size)
-{
-    ram_addr_t addr;
-    if ((last_ram_offset + size) > kqemu_phys_ram_size) {
-        fprintf(stderr, "Not enough memory (requested_size = %" PRIu64 ", max memory = %" PRIu64 ")\n",
-                (uint64_t)size, (uint64_t)kqemu_phys_ram_size);
-        abort();
-    }
-    addr = last_ram_offset;
-    last_ram_offset = TARGET_PAGE_ALIGN(last_ram_offset + size);
-    return addr;
-}
-#endif
-
 ram_addr_t qemu_ram_alloc(ram_addr_t size)
 {
     RAMBlock *new_block;
 
-#ifdef CONFIG_KQEMU
-    if (kqemu_phys_ram_base) {
-        return kqemu_ram_alloc(size);
-    }
-#endif
-
     size = TARGET_PAGE_ALIGN(size);
     new_block = qemu_malloc(sizeof(*new_block));
 
+#if defined(TARGET_S390X) && defined(CONFIG_KVM)
+    /* XXX S390 KVM requires the topmost vma of the RAM to be < 256GB */
+    new_block->host = mmap((void*)0x1000000, size, PROT_EXEC|PROT_READ|PROT_WRITE,
+                           MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+#else
     new_block->host = qemu_vmalloc(size);
+#endif
+#ifdef MADV_MERGEABLE
+    madvise(new_block->host, size, MADV_MERGEABLE);
+#endif
     new_block->offset = last_ram_offset;
     new_block->length = size;
 
@@ -2564,12 +2513,6 @@
     RAMBlock **prevp;
     RAMBlock *block;
 
-#ifdef CONFIG_KQEMU
-    if (kqemu_phys_ram_base) {
-        return kqemu_phys_ram_base + addr;
-    }
-#endif
-
     prev = NULL;
     prevp = &ram_blocks;
     block = ram_blocks;
@@ -2602,12 +2545,6 @@
     RAMBlock *block;
     uint8_t *host = ptr;
 
-#ifdef CONFIG_KQEMU
-    if (kqemu_phys_ram_base) {
-        return host - kqemu_phys_ram_base;
-    }
-#endif
-
     prev = NULL;
     prevp = &ram_blocks;
     block = ram_blocks;
@@ -2630,7 +2567,7 @@
 #ifdef DEBUG_UNASSIGNED
     printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
 #endif
-#if defined(TARGET_SPARC)
+#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
     do_unassigned_access(addr, 0, 0, 0, 1);
 #endif
     return 0;
@@ -2641,7 +2578,7 @@
 #ifdef DEBUG_UNASSIGNED
     printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
 #endif
-#if defined(TARGET_SPARC)
+#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
     do_unassigned_access(addr, 0, 0, 0, 2);
 #endif
     return 0;
@@ -2652,7 +2589,7 @@
 #ifdef DEBUG_UNASSIGNED
     printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
 #endif
-#if defined(TARGET_SPARC)
+#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
     do_unassigned_access(addr, 0, 0, 0, 4);
 #endif
     return 0;
@@ -2663,7 +2600,7 @@
 #ifdef DEBUG_UNASSIGNED
     printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
 #endif
-#if defined(TARGET_SPARC)
+#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
     do_unassigned_access(addr, 1, 0, 0, 1);
 #endif
 }
@@ -2673,7 +2610,7 @@
 #ifdef DEBUG_UNASSIGNED
     printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
 #endif
-#if defined(TARGET_SPARC)
+#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
     do_unassigned_access(addr, 1, 0, 0, 2);
 #endif
 }
@@ -2683,7 +2620,7 @@
 #ifdef DEBUG_UNASSIGNED
     printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
 #endif
-#if defined(TARGET_SPARC)
+#if defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
     do_unassigned_access(addr, 1, 0, 0, 4);
 #endif
 }
@@ -2712,11 +2649,6 @@
 #endif
     }
     stb_p(qemu_get_ram_ptr(ram_addr), val);
-#ifdef CONFIG_KQEMU
-    if (cpu_single_env->kqemu_enabled &&
-        (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
-        kqemu_modify_page(cpu_single_env, ram_addr);
-#endif
     dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
     phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
     /* we remove the notdirty callback only if the code has been
@@ -2737,11 +2669,6 @@
 #endif
     }
     stw_p(qemu_get_ram_ptr(ram_addr), val);
-#ifdef CONFIG_KQEMU
-    if (cpu_single_env->kqemu_enabled &&
-        (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
-        kqemu_modify_page(cpu_single_env, ram_addr);
-#endif
     dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
     phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
     /* we remove the notdirty callback only if the code has been
@@ -2762,11 +2689,6 @@
 #endif
     }
     stl_p(qemu_get_ram_ptr(ram_addr), val);
-#ifdef CONFIG_KQEMU
-    if (cpu_single_env->kqemu_enabled &&
-        (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
-        kqemu_modify_page(cpu_single_env, ram_addr);
-#endif
     dirty_flags |= (0xff & ~CODE_DIRTY_FLAG);
     phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS] = dirty_flags;
     /* we remove the notdirty callback only if the code has been
@@ -2805,7 +2727,7 @@
         return;
     }
     vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
-    TAILQ_FOREACH(wp, &env->watchpoints, entry) {
+    QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
         if ((vaddr == (wp->vaddr & len_mask) ||
              (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) {
             wp->flags |= BP_WATCHPOINT_HIT;
@@ -2995,7 +2917,7 @@
     idx = SUBPAGE_IDX(start);
     eidx = SUBPAGE_IDX(end);
 #if defined(DEBUG_SUBPAGE)
-    printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %d\n", __func__,
+    printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__,
            mmio, start, end, idx, eidx, memory);
 #endif
     memory >>= IO_MEM_SHIFT;
@@ -3047,7 +2969,7 @@
             io_mem_used[i] = 1;
             return i;
         }
-
+    fprintf(stderr, "RAN out out io_mem_idx, max %d !\n", IO_MEM_NB_ENTRIES);
     return -1;
 }
 
@@ -3117,13 +3039,6 @@
 
     io_mem_watch = cpu_register_io_memory(watch_mem_read,
                                           watch_mem_write, NULL);
-#ifdef CONFIG_KQEMU
-    if (kqemu_phys_ram_base) {
-        /* alloc dirty bits array */
-        phys_ram_dirty = qemu_vmalloc(kqemu_phys_ram_size >> TARGET_PAGE_BITS);
-        memset(phys_ram_dirty, 0xff, kqemu_phys_ram_size >> TARGET_PAGE_BITS);
-    }
-#endif
 }
 
 #endif /* !defined(CONFIG_USER_ONLY) */
@@ -3318,11 +3233,11 @@
 typedef struct MapClient {
     void *opaque;
     void (*callback)(void *opaque);
-    LIST_ENTRY(MapClient) link;
+    QLIST_ENTRY(MapClient) link;
 } MapClient;
 
-static LIST_HEAD(map_client_list, MapClient) map_client_list
-    = LIST_HEAD_INITIALIZER(map_client_list);
+static QLIST_HEAD(map_client_list, MapClient) map_client_list
+    = QLIST_HEAD_INITIALIZER(map_client_list);
 
 void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque))
 {
@@ -3330,7 +3245,7 @@
 
     client->opaque = opaque;
     client->callback = callback;
-    LIST_INSERT_HEAD(&map_client_list, client, link);
+    QLIST_INSERT_HEAD(&map_client_list, client, link);
     return client;
 }
 
@@ -3338,17 +3253,18 @@
 {
     MapClient *client = (MapClient *)_client;
 
-    LIST_REMOVE(client, link);
+    QLIST_REMOVE(client, link);
+    qemu_free(client);
 }
 
 static void cpu_notify_map_clients(void)
 {
     MapClient *client;
 
-    while (!LIST_EMPTY(&map_client_list)) {
-        client = LIST_FIRST(&map_client_list);
+    while (!QLIST_EMPTY(&map_client_list)) {
+        client = QLIST_FIRST(&map_client_list);
         client->callback(client->opaque);
-        LIST_REMOVE(client, link);
+        QLIST_REMOVE(client, link);
     }
 }
 
diff --git a/gdbstub.c b/gdbstub.c
index 9bd4375..92a353e 100644
--- a/gdbstub.c
+++ b/gdbstub.c
@@ -2337,7 +2337,7 @@
 static void gdb_chr_event(void *opaque, int event)
 {
     switch (event) {
-    case CHR_EVENT_RESET:
+    case CHR_EVENT_OPENED:
         vm_stop(EXCP_INTERRUPT);
         gdb_has_xml = 0;
         break;
diff --git a/hw/qdev.c b/hw/qdev.c
index 385e709..1c8b981 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -98,7 +98,7 @@
                  t->info->bus_type, bus->type);
     }
     dev->parent_bus = bus;
-    LIST_INSERT_HEAD(&bus->children, dev, sibling);
+    QLIST_INSERT_HEAD(&bus->children, dev, sibling);
     return dev;
 }
 
@@ -113,8 +113,8 @@
 /* Unlink device from bus and free the structure.  */
 void qdev_free(DeviceState *dev)
 {
-    LIST_REMOVE(dev, sibling);
-    free(dev);
+    QLIST_REMOVE(dev, sibling);
+    qemu_free(dev);
 }
 
 static DeviceProperty *create_prop(DeviceState *dev, const char *name,
@@ -293,7 +293,7 @@
 {
     BusState *bus;
 
-    LIST_FOREACH(bus, &dev->child_bus, sibling) {
+    QLIST_FOREACH(bus, &dev->child_bus, sibling) {
         if (strcmp(name, bus->name) == 0) {
             return bus;
         }
@@ -329,9 +329,9 @@
     bus->type = type;
     bus->parent = parent;
     bus->name = qemu_strdup(name);
-    LIST_INIT(&bus->children);
+    QLIST_INIT(&bus->children);
     if (parent) {
-        LIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
+        QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
     }
     return bus;
 }
@@ -384,7 +384,7 @@
     default:
         break;
     }
-    LIST_FOREACH(child, &dev->child_bus, sibling) {
+    QLIST_FOREACH(child, &dev->child_bus, sibling) {
         qbus_print(mon, child, indent);
     }
 }
@@ -396,7 +396,7 @@
     qdev_printf("bus: %s\n", bus->name);
     indent += 2;
     qdev_printf("type %s\n", bus_type_names[bus->type]);
-    LIST_FOREACH(dev, &bus->children, sibling) {
+    QLIST_FOREACH(dev, &bus->children, sibling) {
         qdev_print(mon, dev, indent);
     }
 }
diff --git a/hw/qdev.h b/hw/qdev.h
index ad10499..6dc9dcb 100644
--- a/hw/qdev.h
+++ b/hw/qdev.h
@@ -2,7 +2,7 @@
 #define QDEV_H
 
 #include "hw.h"
-#include "sys-queue.h"
+#include "qemu-queue.h"
 
 typedef struct DeviceType DeviceType;
 
@@ -20,9 +20,9 @@
     qemu_irq *gpio_out;
     int num_gpio_in;
     qemu_irq *gpio_in;
-    LIST_HEAD(, BusState) child_bus;
+    QLIST_HEAD(, BusState) child_bus;
     NICInfo *nd;
-    LIST_ENTRY(DeviceState) sibling;
+    QLIST_ENTRY(DeviceState) sibling;
 };
 
 typedef enum {
@@ -37,8 +37,8 @@
     DeviceState *parent;
     const char *name;
     BusType type;
-    LIST_HEAD(, DeviceState) children;
-    LIST_ENTRY(BusState) sibling;
+    QLIST_HEAD(, DeviceState) children;
+    QLIST_ENTRY(BusState) sibling;
 };
 
 /*** Board API.  This should go away once we have a machine config file.  ***/
diff --git a/hw/watchdog.c b/hw/watchdog.c
index 9a28621..27cd16f 100644
--- a/hw/watchdog.c
+++ b/hw/watchdog.c
@@ -14,23 +14,21 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
- * USA.
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  *
  * By Richard W.M. Jones (rjones@redhat.com).
  */
 
 #include "qemu-common.h"
-#include "sys-queue.h"
+#include "qemu-queue.h"
 #include "sysemu.h"
 #include "hw/watchdog.h"
 
-static LIST_HEAD(watchdog_list, WatchdogTimerModel) watchdog_list;
+static QLIST_HEAD(watchdog_list, WatchdogTimerModel) watchdog_list;
 
 void watchdog_add_model(WatchdogTimerModel *model)
 {
-    LIST_INSERT_HEAD(&watchdog_list, model, entry);
+    QLIST_INSERT_HEAD(&watchdog_list, model, entry);
 }
 
 /* Returns:
@@ -50,14 +48,14 @@
 
     /* -watchdog ? lists available devices and exits cleanly. */
     if (strcmp(p, "?") == 0) {
-        LIST_FOREACH(model, &watchdog_list, entry) {
+        QLIST_FOREACH(model, &watchdog_list, entry) {
             fprintf(stderr, "\t%s\t%s\n",
                      model->wdt_name, model->wdt_description);
         }
         return 2;
     }
 
-    LIST_FOREACH(model, &watchdog_list, entry) {
+    QLIST_FOREACH(model, &watchdog_list, entry) {
         if (strcasecmp(model->wdt_name, p) == 0) {
             watchdog = model;
             return 0;
@@ -65,7 +63,7 @@
     }
 
     fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n");
-    LIST_FOREACH(model, &watchdog_list, entry) {
+    QLIST_FOREACH(model, &watchdog_list, entry) {
         fprintf(stderr, "\t%s\t%s\n",
                  model->wdt_name, model->wdt_description);
     }
diff --git a/hw/watchdog.h b/hw/watchdog.h
index c2b2b36..77b9965 100644
--- a/hw/watchdog.h
+++ b/hw/watchdog.h
@@ -36,7 +36,7 @@
 #define WDT_NONE         6	/* Do nothing. */
 
 struct WatchdogTimerModel {
-    LIST_ENTRY(WatchdogTimerModel) entry;
+    QLIST_ENTRY(WatchdogTimerModel) entry;
 
     /* Short name of the device - used to select it on the command line. */
     const char *wdt_name;
diff --git a/kvm.h b/kvm.h
index 560aef3..f5f5a55 100644
--- a/kvm.h
+++ b/kvm.h
@@ -15,7 +15,7 @@
 #define QEMU_KVM_H
 
 #include "config.h"
-#include "sys-queue.h"
+#include "qemu-queue.h"
 
 #ifdef CONFIG_KVM
 extern int kvm_allowed;
@@ -98,10 +98,10 @@
     target_ulong pc;
     target_ulong saved_insn;
     int use_count;
-    TAILQ_ENTRY(kvm_sw_breakpoint) entry;
+    QTAILQ_ENTRY(kvm_sw_breakpoint) entry;
 };
 
-TAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
+QTAILQ_HEAD(kvm_sw_breakpoint_head, kvm_sw_breakpoint);
 
 int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info);
 
diff --git a/memcheck/memcheck.c b/memcheck/memcheck.c
index 9308c9f..8e0a1f8 100644
--- a/memcheck/memcheck.c
+++ b/memcheck/memcheck.c
@@ -20,7 +20,7 @@
 #error CONFIG_MEMCHECK is not defined.
 #endif  // CONFIG_MEMCHECK
 
-#include "sys-queue.h"
+#include "qemu-queue.h"
 #include "qemu_file.h"
 #include "elff_api.h"
 #include "memcheck.h"
diff --git a/memcheck/memcheck_proc_management.c b/memcheck/memcheck_proc_management.c
index 531ec4a..4120b9f 100644
--- a/memcheck/memcheck_proc_management.c
+++ b/memcheck/memcheck_proc_management.c
@@ -49,10 +49,10 @@
 static ProcDesc*    current_process = NULL;
 
 /* List of running processes. */
-static LIST_HEAD(proc_list, ProcDesc) proc_list;
+static QLIST_HEAD(proc_list, ProcDesc) proc_list;
 
 /* List of running threads. */
-static LIST_HEAD(thread_list, ThreadDesc) thread_list;
+static QLIST_HEAD(thread_list, ThreadDesc) thread_list;
 
 // =============================================================================
 // Static routines
@@ -83,8 +83,8 @@
     new_thread->call_stack = NULL;
     new_thread->call_stack_count = 0;
     new_thread->call_stack_max = 0;
-    LIST_INSERT_HEAD(&thread_list, new_thread, global_entry);
-    LIST_INSERT_HEAD(&proc->threads, new_thread, proc_entry);
+    QLIST_INSERT_HEAD(&thread_list, new_thread, global_entry);
+    QLIST_INSERT_HEAD(&proc->threads, new_thread, proc_entry);
     return new_thread;
 }
 
@@ -108,7 +108,7 @@
         ME("memcheck: Unable to allocate new process descriptor");
         return NULL;
     }
-    LIST_INIT(&new_proc->threads);
+    QLIST_INIT(&new_proc->threads);
     allocmap_init(&new_proc->alloc_map);
     mmrangemap_init(&new_proc->mmrange_map);
     new_proc->pid = pid;
@@ -164,7 +164,7 @@
     }
 
     // List new process.
-    LIST_INSERT_HEAD(&proc_list, new_proc, global_entry);
+    QLIST_INSERT_HEAD(&proc_list, new_proc, global_entry);
 
     return new_proc;
 }
@@ -188,7 +188,7 @@
         return current_thread;
     }
 
-    LIST_FOREACH(thread, &thread_list, global_entry) {
+    QLIST_FOREACH(thread, &thread_list, global_entry) {
         if (tid == thread->tid) {
             if (tid == current_tid) {
                 current_thread = thread;
@@ -213,7 +213,7 @@
          * optimize this code for performance, as this routine is called from
          * the performance sensitive path. */
         ThreadDesc* thread;
-        LIST_FOREACH(thread, &thread_list, global_entry) {
+        QLIST_FOREACH(thread, &thread_list, global_entry) {
             if (current_tid == thread->tid) {
                 current_thread = thread;
                 return current_thread;
@@ -313,8 +313,8 @@
 void
 memcheck_init_proc_management(void)
 {
-    LIST_INIT(&proc_list);
-    LIST_INIT(&thread_list);
+    QLIST_INIT(&proc_list);
+    QLIST_INIT(&thread_list);
 }
 
 ProcDesc*
@@ -329,7 +329,7 @@
         return current_process;
     }
 
-    LIST_FOREACH(proc, &proc_list, global_entry) {
+    QLIST_FOREACH(proc, &proc_list, global_entry) {
         if (pid == proc->pid) {
             break;
         }
@@ -597,13 +597,13 @@
     current_thread = NULL;
 
     // Unlist the thread from its process as well as global lists.
-    LIST_REMOVE(thread, proc_entry);
-    LIST_REMOVE(thread, global_entry);
+    QLIST_REMOVE(thread, proc_entry);
+    QLIST_REMOVE(thread, global_entry);
     threaddesc_free(thread);
 
     /* Lets see if this was last process thread, which would indicate
      * process termination. */
-    if (!LIST_EMPTY(&proc->threads)) {
+    if (!QLIST_EMPTY(&proc->threads)) {
         return;
     }
 
@@ -686,7 +686,7 @@
     /* Since current process is exiting, we need to NULL its cached descriptor,
      * and unlist it from the list of running processes. */
     current_process = NULL;
-    LIST_REMOVE(proc, global_entry);
+    QLIST_REMOVE(proc, global_entry);
 
     // Empty process' mmapings map.
     mmrangemap_empty(&proc->mmrange_map);
diff --git a/memcheck/memcheck_proc_management.h b/memcheck/memcheck_proc_management.h
index d5525b1..68b6181 100644
--- a/memcheck/memcheck_proc_management.h
+++ b/memcheck/memcheck_proc_management.h
@@ -24,7 +24,7 @@
 #error CONFIG_MEMCHECK is not defined.
 #endif  // CONFIG_MEMCHECK
 
-#include "sys-queue.h"
+#include "qemu-queue.h"
 #include "memcheck_common.h"
 #include "memcheck_malloc_map.h"
 #include "memcheck_mmrange_map.h"
@@ -46,10 +46,10 @@
     MMRangeMap                                  mmrange_map;
 
     /* Descriptor's entry in the global process list. */
-    LIST_ENTRY(ProcDesc)                        global_entry;
+    QLIST_ENTRY(ProcDesc)                        global_entry;
 
     /* List of threads running in context of this process. */
-    LIST_HEAD(threads, ThreadDesc)              threads;
+    QLIST_HEAD(threads, ThreadDesc)              threads;
 
     /* Path to the process' image file. */
     char*                                       image_path;
@@ -92,10 +92,10 @@
 /* Describes a thread that is monitored by memchecker framework. */
 typedef struct ThreadDesc {
     /* Descriptor's entry in the global thread list. */
-    LIST_ENTRY(ThreadDesc)  global_entry;
+    QLIST_ENTRY(ThreadDesc)  global_entry;
 
     /* Descriptor's entry in the process' thread list. */
-    LIST_ENTRY(ThreadDesc)  proc_entry;
+    QLIST_ENTRY(ThreadDesc)  proc_entry;
 
     /* Descriptor of the process this thread belongs to. */
     ProcDesc*               process;
diff --git a/module.c b/module.c
index 3729283..e77d569 100644
--- a/module.c
+++ b/module.c
@@ -12,17 +12,17 @@
  */
 
 #include "qemu-common.h"
-#include "sys-queue.h"
+#include "qemu-queue.h"
 #include "module.h"
 
 typedef struct ModuleEntry
 {
     module_init_type type;
     void (*init)(void);
-    TAILQ_ENTRY(ModuleEntry) node;
+    QTAILQ_ENTRY(ModuleEntry) node;
 } ModuleEntry;
 
-typedef TAILQ_HEAD(, ModuleEntry) ModuleTypeList;
+typedef QTAILQ_HEAD(, ModuleEntry) ModuleTypeList;
 
 static ModuleTypeList init_type_list[MODULE_INIT_MAX];
 
@@ -36,7 +36,7 @@
     }
 
     for (i = 0; i < MODULE_INIT_MAX; i++) {
-        TAILQ_INIT(&init_type_list[i]);
+        QTAILQ_INIT(&init_type_list[i]);
     }
 
     inited = 1;
@@ -64,7 +64,7 @@
 
     l = find_type(type);
 
-    TAILQ_INSERT_TAIL(l, e, node);
+    QTAILQ_INSERT_TAIL(l, e, node);
 }
 
 void module_call_init(module_init_type type)
@@ -74,7 +74,7 @@
 
     l = find_type(type);
 
-    TAILQ_FOREACH(e, l, node) {
+    QTAILQ_FOREACH(e, l, node) {
         e->init();
     }
 }
diff --git a/monitor.c b/monitor.c
index 6b45f6c..0ef5d963 100644
--- a/monitor.c
+++ b/monitor.c
@@ -80,10 +80,10 @@
     CPUState *mon_cpu;
     BlockDriverCompletionFunc *password_completion_cb;
     void *password_opaque;
-    LIST_ENTRY(Monitor) entry;
+    QLIST_ENTRY(Monitor) entry;
 };
 
-static LIST_HEAD(mon_list, Monitor) mon_list;
+static QLIST_HEAD(mon_list, Monitor) mon_list;
 
 static const mon_cmd_t mon_cmds[];
 static const mon_cmd_t info_cmds[];
@@ -1483,7 +1483,7 @@
 #endif
 
 /* Capture support */
-static LIST_HEAD (capture_list_head, CaptureState) capture_head;
+static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
 
 static void do_info_capture(Monitor *mon)
 {
@@ -1505,7 +1505,7 @@
     for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
         if (i == n) {
             s->ops.destroy (s->opaque);
-            LIST_REMOVE (s, entries);
+            QLIST_REMOVE (s, entries);
             qemu_free (s);
             return;
         }
@@ -1529,7 +1529,7 @@
         monitor_printf(mon, "Faied to add wave capture\n");
         qemu_free (s);
     }
-    LIST_INSERT_HEAD (&capture_head, s, entries);
+    QLIST_INSERT_HEAD (&capture_head, s, entries);
 }
 #endif
 
@@ -1599,7 +1599,7 @@
         qemu_acl_entry *entry;
         monitor_printf(mon, "policy: %s\n",
                        acl->defaultDeny ? "deny" : "allow");
-        TAILQ_FOREACH(entry, &acl->entries, next) {
+        QTAILQ_FOREACH(entry, &acl->entries, next) {
             i++;
             monitor_printf(mon, "%d: %s %s\n", i,
                            entry->deny ? "deny" : "allow",
@@ -2999,7 +2999,7 @@
         monitor_suspend(mon);
         break;
 
-    case CHR_EVENT_RESET:
+    case CHR_EVENT_OPENED:
         monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
                        "information\n", QEMU_VERSION);
         if (mon->chr->focus == 0)
@@ -3041,7 +3041,7 @@
     qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
                           mon);
 
-    LIST_INSERT_HEAD(&mon_list, mon, entry);
+    QLIST_INSERT_HEAD(&mon_list, mon, entry);
     if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
         cur_mon = mon;
 }
diff --git a/qemu-char-android.c b/qemu-char-android.c
index 45c81e5..b468606 100644
--- a/qemu-char-android.c
+++ b/qemu-char-android.c
@@ -114,8 +114,8 @@
 /***********************************************************/
 /* character device */
 
-static TAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
-    TAILQ_HEAD_INITIALIZER(chardevs);
+static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
+    QTAILQ_HEAD_INITIALIZER(chardevs);
 static int initial_reset_issued;
 
 static void qemu_chr_event(CharDriverState *s, int event)
@@ -128,7 +128,7 @@
 static void qemu_chr_reset_bh(void *opaque)
 {
     CharDriverState *s = opaque;
-    qemu_chr_event(s, CHR_EVENT_RESET);
+    qemu_chr_event(s, CHR_EVENT_OPENED);
     qemu_bh_delete(s->bh);
     s->bh = NULL;
 }
@@ -147,7 +147,7 @@
 
     initial_reset_issued = 1;
 
-    TAILQ_FOREACH(chr, &chardevs, next) {
+    QTAILQ_FOREACH(chr, &chardevs, next) {
         qemu_chr_reset(chr);
     }
 }
@@ -2243,14 +2243,14 @@
             chr->filename = qemu_strdup(filename);
         chr->init = init;
         chr->label = qemu_strdup(label);
-        TAILQ_INSERT_TAIL(&chardevs, chr, next);
+        QTAILQ_INSERT_TAIL(&chardevs, chr, next);
     }
     return chr;
 }
 
 void qemu_chr_close(CharDriverState *chr)
 {
-    TAILQ_REMOVE(&chardevs, chr, next);
+    QTAILQ_REMOVE(&chardevs, chr, next);
     if (chr->chr_close)
         chr->chr_close(chr);
     qemu_free(chr->filename);
@@ -2262,7 +2262,7 @@
 {
     CharDriverState *chr;
 
-    TAILQ_FOREACH(chr, &chardevs, next) {
+    QTAILQ_FOREACH(chr, &chardevs, next) {
         monitor_printf(mon, "%s: filename=%s\n", chr->label, chr->filename);
     }
 }
diff --git a/qemu-char.c b/qemu-char.c
index 37f50a9..5901faf 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -120,7 +120,7 @@
 static void qemu_chr_reset_bh(void *opaque)
 {
     CharDriverState *s = opaque;
-    qemu_chr_event(s, CHR_EVENT_RESET);
+    qemu_chr_event(s, CHR_EVENT_OPENED);
     qemu_bh_delete(s->bh);
     s->bh = NULL;
 }
@@ -139,7 +139,7 @@
 
     initial_reset_issued = 1;
 
-    TAILQ_FOREACH(chr, &chardevs, next) {
+    QTAILQ_FOREACH(chr, &chardevs, next) {
         qemu_chr_reset(chr);
     }
 }
@@ -2238,7 +2238,7 @@
 {
     CharDriverState *chr;
 
-    TAILQ_FOREACH(chr, &chardevs, next) {
+    QTAILQ_FOREACH(chr, &chardevs, next) {
         monitor_printf(mon, "%s: filename=%s\n", chr->label, chr->filename);
     }
 }
diff --git a/qemu-char.h b/qemu-char.h
index e1aa8db..6e1c779 100644
--- a/qemu-char.h
+++ b/qemu-char.h
@@ -2,15 +2,16 @@
 #define QEMU_CHAR_H
 
 #include "qemu-common.h"
-#include "sys-queue.h"
+#include "qemu-queue.h"
 
 /* character device */
 
 #define CHR_EVENT_BREAK   0 /* serial break char */
 #define CHR_EVENT_FOCUS   1 /* focus to this terminal (modal input needed) */
-#define CHR_EVENT_RESET   2 /* new connection established */
+#define CHR_EVENT_OPENED  2 /* new connection established */
 #define CHR_EVENT_MUX_IN  3 /* mux-focus was set to this terminal */
 #define CHR_EVENT_MUX_OUT 4 /* mux-focus will move on */
+#define CHR_EVENT_CLOSED  5 /* connection closed */
 
 
 #define CHR_IOCTL_SERIAL_SET_PARAMS   1
@@ -63,7 +64,7 @@
     QEMUBH *bh;
     char *label;
     char *filename;
-    TAILQ_ENTRY(CharDriverState) next;
+    QTAILQ_ENTRY(CharDriverState) next;
 };
 
 CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s));
diff --git a/sys-queue.h b/qemu-queue.h
similarity index 61%
rename from sys-queue.h
rename to qemu-queue.h
index cb6a4c8..1d07745 100644
--- a/sys-queue.h
+++ b/qemu-queue.h
@@ -1,8 +1,9 @@
-/*      $NetBSD: queue.h,v 1.45.14.1 2007/07/18 20:13:24 liamjfoy Exp $ */
+/*      $NetBSD: queue.h,v 1.52 2009/04/20 09:56:08 mschuett Exp $ */
 
 /*
  * Qemu version: Copy from netbsd, removed debug code, removed some of
- * the implementations.  Left in lists, tail queues and circular queues.
+ * the implementations.  Left in lists, simple queues, tail queues and
+ * circular queues.
  */
 
 /*
@@ -36,12 +37,12 @@
  *      @(#)queue.h     8.5 (Berkeley) 8/20/94
  */
 
-#ifndef _SYS_QUEUE_H_
-#define _SYS_QUEUE_H_
+#ifndef QEMU_SYS_QUEUE_H_
+#define QEMU_SYS_QUEUE_H_
 
 /*
- * This file defines three types of data structures:
- * lists, tail queues, and circular queues.
+ * This file defines four types of data structures:
+ * lists, simple queues, tail queues, and circular queues.
  *
  * A list is headed by a single forward pointer (or an array of forward
  * pointers for a hash table header). The elements are doubly linked
@@ -50,6 +51,13 @@
  * or after an existing element or at the head of the list. A list
  * may only be traversed in the forward direction.
  *
+ * A simple queue is headed by a pair of pointers, one the head of the
+ * list and the other to the tail of the list. The elements are singly
+ * linked to save space, so elements can only be removed from the
+ * head of the list. New elements can be added to the list after
+ * an existing element, at the head of the list, or at the end of the
+ * list. A simple queue may only be traversed in the forward direction.
+ *
  * A tail queue is headed by a pair of pointers, one to the head of the
  * list and the other to the tail of the list. The elements are doubly
  * linked so that an arbitrary element can be removed without a need to
@@ -71,15 +79,15 @@
 /*
  * List definitions.
  */
-#define LIST_HEAD(name, type)                                           \
+#define QLIST_HEAD(name, type)                                          \
 struct name {                                                           \
         struct type *lh_first;  /* first element */                     \
 }
 
-#define LIST_HEAD_INITIALIZER(head)                                     \
+#define QLIST_HEAD_INITIALIZER(head)                                    \
         { NULL }
 
-#define LIST_ENTRY(type)                                                \
+#define QLIST_ENTRY(type)                                               \
 struct {                                                                \
         struct type *le_next;   /* next element */                      \
         struct type **le_prev;  /* address of previous next element */  \
@@ -88,11 +96,11 @@
 /*
  * List functions.
  */
-#define LIST_INIT(head) do {                                            \
+#define QLIST_INIT(head) do {                                           \
         (head)->lh_first = NULL;                                        \
 } while (/*CONSTCOND*/0)
 
-#define LIST_INSERT_AFTER(listelm, elm, field) do {                     \
+#define QLIST_INSERT_AFTER(listelm, elm, field) do {                    \
         if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)  \
                 (listelm)->field.le_next->field.le_prev =               \
                     &(elm)->field.le_next;                              \
@@ -100,69 +108,167 @@
         (elm)->field.le_prev = &(listelm)->field.le_next;               \
 } while (/*CONSTCOND*/0)
 
-#define LIST_INSERT_BEFORE(listelm, elm, field) do {                    \
+#define QLIST_INSERT_BEFORE(listelm, elm, field) do {                   \
         (elm)->field.le_prev = (listelm)->field.le_prev;                \
         (elm)->field.le_next = (listelm);                               \
         *(listelm)->field.le_prev = (elm);                              \
         (listelm)->field.le_prev = &(elm)->field.le_next;               \
 } while (/*CONSTCOND*/0)
 
-#define LIST_INSERT_HEAD(head, elm, field) do {                         \
+#define QLIST_INSERT_HEAD(head, elm, field) do {                        \
         if (((elm)->field.le_next = (head)->lh_first) != NULL)          \
                 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
         (head)->lh_first = (elm);                                       \
         (elm)->field.le_prev = &(head)->lh_first;                       \
 } while (/*CONSTCOND*/0)
 
-#define LIST_REMOVE(elm, field) do {                                    \
+#define QLIST_REMOVE(elm, field) do {                                   \
         if ((elm)->field.le_next != NULL)                               \
                 (elm)->field.le_next->field.le_prev =                   \
                     (elm)->field.le_prev;                               \
         *(elm)->field.le_prev = (elm)->field.le_next;                   \
 } while (/*CONSTCOND*/0)
 
-#define LIST_FOREACH(var, head, field)                                  \
+#define QLIST_FOREACH(var, head, field)                                 \
         for ((var) = ((head)->lh_first);                                \
                 (var);                                                  \
                 (var) = ((var)->field.le_next))
 
+#define QLIST_FOREACH_SAFE(var, head, field, next_var)                  \
+        for ((var) = ((head)->lh_first);                                \
+                (var) && ((next_var) = ((var)->field.le_next), 1);      \
+                (var) = (next_var))
+
 /*
  * List access methods.
  */
-#define LIST_EMPTY(head)                ((head)->lh_first == NULL)
-#define LIST_FIRST(head)                ((head)->lh_first)
-#define LIST_NEXT(elm, field)           ((elm)->field.le_next)
+#define QLIST_EMPTY(head)                ((head)->lh_first == NULL)
+#define QLIST_FIRST(head)                ((head)->lh_first)
+#define QLIST_NEXT(elm, field)           ((elm)->field.le_next)
+
+
+/*
+ * Simple queue definitions.
+ */
+#define QSIMPLEQ_HEAD(name, type)                                       \
+struct name {                                                           \
+    struct type *sqh_first;    /* first element */                      \
+    struct type **sqh_last;    /* addr of last next element */          \
+}
+
+#define QSIMPLEQ_HEAD_INITIALIZER(head)                                 \
+    { NULL, &(head).sqh_first }
+
+#define QSIMPLEQ_ENTRY(type)                                            \
+struct {                                                                \
+    struct type *sqe_next;    /* next element */                        \
+}
+
+/*
+ * Simple queue functions.
+ */
+#define QSIMPLEQ_INIT(head) do {                                        \
+    (head)->sqh_first = NULL;                                           \
+    (head)->sqh_last = &(head)->sqh_first;                              \
+} while (/*CONSTCOND*/0)
+
+#define QSIMPLEQ_INSERT_HEAD(head, elm, field) do {                     \
+    if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)            \
+        (head)->sqh_last = &(elm)->field.sqe_next;                      \
+    (head)->sqh_first = (elm);                                          \
+} while (/*CONSTCOND*/0)
+
+#define QSIMPLEQ_INSERT_TAIL(head, elm, field) do {                     \
+    (elm)->field.sqe_next = NULL;                                       \
+    *(head)->sqh_last = (elm);                                          \
+    (head)->sqh_last = &(elm)->field.sqe_next;                          \
+} while (/*CONSTCOND*/0)
+
+#define QSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {           \
+    if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)    \
+        (head)->sqh_last = &(elm)->field.sqe_next;                      \
+    (listelm)->field.sqe_next = (elm);                                  \
+} while (/*CONSTCOND*/0)
+
+#define QSIMPLEQ_REMOVE_HEAD(head, field) do {                          \
+    if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
+        (head)->sqh_last = &(head)->sqh_first;                          \
+} while (/*CONSTCOND*/0)
+
+#define QSIMPLEQ_REMOVE(head, elm, type, field) do {                    \
+    if ((head)->sqh_first == (elm)) {                                   \
+        QSIMPLEQ_REMOVE_HEAD((head), field);                            \
+    } else {                                                            \
+        struct type *curelm = (head)->sqh_first;                        \
+        while (curelm->field.sqe_next != (elm))                         \
+            curelm = curelm->field.sqe_next;                            \
+        if ((curelm->field.sqe_next =                                   \
+            curelm->field.sqe_next->field.sqe_next) == NULL)            \
+                (head)->sqh_last = &(curelm)->field.sqe_next;           \
+    }                                                                   \
+} while (/*CONSTCOND*/0)
+
+#define QSIMPLEQ_FOREACH(var, head, field)                              \
+    for ((var) = ((head)->sqh_first);                                   \
+        (var);                                                          \
+        (var) = ((var)->field.sqe_next))
+
+#define QSIMPLEQ_FOREACH_SAFE(var, head, field, next)                   \
+    for ((var) = ((head)->sqh_first);                                   \
+        (var) && ((next = ((var)->field.sqe_next)), 1);                 \
+        (var) = (next))
+
+#define QSIMPLEQ_CONCAT(head1, head2) do {                              \
+    if (!QSIMPLEQ_EMPTY((head2))) {                                     \
+        *(head1)->sqh_last = (head2)->sqh_first;                        \
+        (head1)->sqh_last = (head2)->sqh_last;                          \
+        QSIMPLEQ_INIT((head2));                                         \
+    }                                                                   \
+} while (/*CONSTCOND*/0)
+
+#define QSIMPLEQ_LAST(head, type, field)                                \
+    (QSIMPLEQ_EMPTY((head)) ?                                           \
+        NULL :                                                          \
+            ((struct type *)(void *)                                    \
+        ((char *)((head)->sqh_last) - offsetof(struct type, field))))
+
+/*
+ * Simple queue access methods.
+ */
+#define QSIMPLEQ_EMPTY(head)        ((head)->sqh_first == NULL)
+#define QSIMPLEQ_FIRST(head)        ((head)->sqh_first)
+#define QSIMPLEQ_NEXT(elm, field)   ((elm)->field.sqe_next)
 
 
 /*
  * Tail queue definitions.
  */
-#define _TAILQ_HEAD(name, type, qual)                                   \
+#define Q_TAILQ_HEAD(name, type, qual)                                  \
 struct name {                                                           \
         qual type *tqh_first;           /* first element */             \
         qual type *qual *tqh_last;      /* addr of last next element */ \
 }
-#define TAILQ_HEAD(name, type)  _TAILQ_HEAD(name, struct type,)
+#define QTAILQ_HEAD(name, type)  Q_TAILQ_HEAD(name, struct type,)
 
-#define TAILQ_HEAD_INITIALIZER(head)                                    \
+#define QTAILQ_HEAD_INITIALIZER(head)                                   \
         { NULL, &(head).tqh_first }
 
-#define _TAILQ_ENTRY(type, qual)                                        \
+#define Q_TAILQ_ENTRY(type, qual)                                       \
 struct {                                                                \
         qual type *tqe_next;            /* next element */              \
         qual type *qual *tqe_prev;      /* address of previous next element */\
 }
-#define TAILQ_ENTRY(type)       _TAILQ_ENTRY(struct type,)
+#define QTAILQ_ENTRY(type)       Q_TAILQ_ENTRY(struct type,)
 
 /*
  * Tail queue functions.
  */
-#define TAILQ_INIT(head) do {                                           \
+#define QTAILQ_INIT(head) do {                                          \
         (head)->tqh_first = NULL;                                       \
         (head)->tqh_last = &(head)->tqh_first;                          \
 } while (/*CONSTCOND*/0)
 
-#define TAILQ_INSERT_HEAD(head, elm, field) do {                        \
+#define QTAILQ_INSERT_HEAD(head, elm, field) do {                       \
         if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)        \
                 (head)->tqh_first->field.tqe_prev =                     \
                     &(elm)->field.tqe_next;                             \
@@ -172,14 +278,14 @@
         (elm)->field.tqe_prev = &(head)->tqh_first;                     \
 } while (/*CONSTCOND*/0)
 
-#define TAILQ_INSERT_TAIL(head, elm, field) do {                        \
+#define QTAILQ_INSERT_TAIL(head, elm, field) do {                       \
         (elm)->field.tqe_next = NULL;                                   \
         (elm)->field.tqe_prev = (head)->tqh_last;                       \
         *(head)->tqh_last = (elm);                                      \
         (head)->tqh_last = &(elm)->field.tqe_next;                      \
 } while (/*CONSTCOND*/0)
 
-#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {              \
+#define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do {             \
         if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
                 (elm)->field.tqe_next->field.tqe_prev =                 \
                     &(elm)->field.tqe_next;                             \
@@ -189,14 +295,14 @@
         (elm)->field.tqe_prev = &(listelm)->field.tqe_next;             \
 } while (/*CONSTCOND*/0)
 
-#define TAILQ_INSERT_BEFORE(listelm, elm, field) do {                   \
+#define QTAILQ_INSERT_BEFORE(listelm, elm, field) do {                  \
         (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
         (elm)->field.tqe_next = (listelm);                              \
         *(listelm)->field.tqe_prev = (elm);                             \
         (listelm)->field.tqe_prev = &(elm)->field.tqe_next;             \
 } while (/*CONSTCOND*/0)
 
-#define TAILQ_REMOVE(head, elm, field) do {                             \
+#define QTAILQ_REMOVE(head, elm, field) do {                            \
         if (((elm)->field.tqe_next) != NULL)                            \
                 (elm)->field.tqe_next->field.tqe_prev =                 \
                     (elm)->field.tqe_prev;                              \
@@ -205,17 +311,17 @@
         *(elm)->field.tqe_prev = (elm)->field.tqe_next;                 \
 } while (/*CONSTCOND*/0)
 
-#define TAILQ_FOREACH(var, head, field)                                 \
+#define QTAILQ_FOREACH(var, head, field)                                \
         for ((var) = ((head)->tqh_first);                               \
                 (var);                                                  \
                 (var) = ((var)->field.tqe_next))
 
-#define TAILQ_FOREACH_SAFE(var, head, field, next_var)                  \
+#define QTAILQ_FOREACH_SAFE(var, head, field, next_var)                 \
         for ((var) = ((head)->tqh_first);                               \
                 (var) && ((next_var) = ((var)->field.tqe_next), 1);     \
                 (var) = (next_var))
 
-#define TAILQ_FOREACH_REVERSE(var, head, headname, field)               \
+#define QTAILQ_FOREACH_REVERSE(var, head, headname, field)              \
         for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last));    \
                 (var);                                                  \
                 (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
@@ -223,29 +329,29 @@
 /*
  * Tail queue access methods.
  */
-#define TAILQ_EMPTY(head)               ((head)->tqh_first == NULL)
-#define TAILQ_FIRST(head)               ((head)->tqh_first)
-#define TAILQ_NEXT(elm, field)          ((elm)->field.tqe_next)
+#define QTAILQ_EMPTY(head)               ((head)->tqh_first == NULL)
+#define QTAILQ_FIRST(head)               ((head)->tqh_first)
+#define QTAILQ_NEXT(elm, field)          ((elm)->field.tqe_next)
 
-#define TAILQ_LAST(head, headname) \
+#define QTAILQ_LAST(head, headname) \
         (*(((struct headname *)((head)->tqh_last))->tqh_last))
-#define TAILQ_PREV(elm, headname, field) \
+#define QTAILQ_PREV(elm, headname, field) \
         (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
 
 
 /*
  * Circular queue definitions.
  */
-#define CIRCLEQ_HEAD(name, type)                                        \
+#define QCIRCLEQ_HEAD(name, type)                                       \
 struct name {                                                           \
         struct type *cqh_first;         /* first element */             \
         struct type *cqh_last;          /* last element */              \
 }
 
-#define CIRCLEQ_HEAD_INITIALIZER(head)                                  \
+#define QCIRCLEQ_HEAD_INITIALIZER(head)                                 \
         { (void *)&head, (void *)&head }
 
-#define CIRCLEQ_ENTRY(type)                                             \
+#define QCIRCLEQ_ENTRY(type)                                            \
 struct {                                                                \
         struct type *cqe_next;          /* next element */              \
         struct type *cqe_prev;          /* previous element */          \
@@ -254,12 +360,12 @@
 /*
  * Circular queue functions.
  */
-#define CIRCLEQ_INIT(head) do {                                         \
+#define QCIRCLEQ_INIT(head) do {                                        \
         (head)->cqh_first = (void *)(head);                             \
         (head)->cqh_last = (void *)(head);                              \
 } while (/*CONSTCOND*/0)
 
-#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {            \
+#define QCIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {           \
         (elm)->field.cqe_next = (listelm)->field.cqe_next;              \
         (elm)->field.cqe_prev = (listelm);                              \
         if ((listelm)->field.cqe_next == (void *)(head))                \
@@ -269,7 +375,7 @@
         (listelm)->field.cqe_next = (elm);                              \
 } while (/*CONSTCOND*/0)
 
-#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {           \
+#define QCIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {          \
         (elm)->field.cqe_next = (listelm);                              \
         (elm)->field.cqe_prev = (listelm)->field.cqe_prev;              \
         if ((listelm)->field.cqe_prev == (void *)(head))                \
@@ -279,7 +385,7 @@
         (listelm)->field.cqe_prev = (elm);                              \
 } while (/*CONSTCOND*/0)
 
-#define CIRCLEQ_INSERT_HEAD(head, elm, field) do {                      \
+#define QCIRCLEQ_INSERT_HEAD(head, elm, field) do {                     \
         (elm)->field.cqe_next = (head)->cqh_first;                      \
         (elm)->field.cqe_prev = (void *)(head);                         \
         if ((head)->cqh_last == (void *)(head))                         \
@@ -289,7 +395,7 @@
         (head)->cqh_first = (elm);                                      \
 } while (/*CONSTCOND*/0)
 
-#define CIRCLEQ_INSERT_TAIL(head, elm, field) do {                      \
+#define QCIRCLEQ_INSERT_TAIL(head, elm, field) do {                     \
         (elm)->field.cqe_next = (void *)(head);                         \
         (elm)->field.cqe_prev = (head)->cqh_last;                       \
         if ((head)->cqh_first == (void *)(head))                        \
@@ -299,7 +405,7 @@
         (head)->cqh_last = (elm);                                       \
 } while (/*CONSTCOND*/0)
 
-#define CIRCLEQ_REMOVE(head, elm, field) do {                           \
+#define QCIRCLEQ_REMOVE(head, elm, field) do {                          \
         if ((elm)->field.cqe_next == (void *)(head))                    \
                 (head)->cqh_last = (elm)->field.cqe_prev;               \
         else                                                            \
@@ -312,12 +418,12 @@
                     (elm)->field.cqe_next;                              \
 } while (/*CONSTCOND*/0)
 
-#define CIRCLEQ_FOREACH(var, head, field)                               \
+#define QCIRCLEQ_FOREACH(var, head, field)                              \
         for ((var) = ((head)->cqh_first);                               \
                 (var) != (const void *)(head);                          \
                 (var) = ((var)->field.cqe_next))
 
-#define CIRCLEQ_FOREACH_REVERSE(var, head, field)                       \
+#define QCIRCLEQ_FOREACH_REVERSE(var, head, field)                      \
         for ((var) = ((head)->cqh_last);                                \
                 (var) != (const void *)(head);                          \
                 (var) = ((var)->field.cqe_prev))
@@ -325,19 +431,19 @@
 /*
  * Circular queue access methods.
  */
-#define CIRCLEQ_EMPTY(head)             ((head)->cqh_first == (void *)(head))
-#define CIRCLEQ_FIRST(head)             ((head)->cqh_first)
-#define CIRCLEQ_LAST(head)              ((head)->cqh_last)
-#define CIRCLEQ_NEXT(elm, field)        ((elm)->field.cqe_next)
-#define CIRCLEQ_PREV(elm, field)        ((elm)->field.cqe_prev)
+#define QCIRCLEQ_EMPTY(head)             ((head)->cqh_first == (void *)(head))
+#define QCIRCLEQ_FIRST(head)             ((head)->cqh_first)
+#define QCIRCLEQ_LAST(head)              ((head)->cqh_last)
+#define QCIRCLEQ_NEXT(elm, field)        ((elm)->field.cqe_next)
+#define QCIRCLEQ_PREV(elm, field)        ((elm)->field.cqe_prev)
 
-#define CIRCLEQ_LOOP_NEXT(head, elm, field)                             \
+#define QCIRCLEQ_LOOP_NEXT(head, elm, field)                            \
         (((elm)->field.cqe_next == (void *)(head))                      \
             ? ((head)->cqh_first)                                       \
             : (elm->field.cqe_next))
-#define CIRCLEQ_LOOP_PREV(head, elm, field)                             \
+#define QCIRCLEQ_LOOP_PREV(head, elm, field)                            \
         (((elm)->field.cqe_prev == (void *)(head))                      \
             ? ((head)->cqh_last)                                        \
             : (elm->field.cqe_prev))
 
-#endif  /* !_SYS_QUEUE_H_ */
+#endif  /* !QEMU_SYS_QUEUE_H_ */
diff --git a/target-arm/translate.c b/target-arm/translate.c
index 84600f7..275356d 100644
--- a/target-arm/translate.c
+++ b/target-arm/translate.c
@@ -16,8 +16,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 #include <stdarg.h>
 #include <stdlib.h>
@@ -8935,8 +8934,8 @@
         }
 #endif
 
-        if (unlikely(!TAILQ_EMPTY(&env->breakpoints))) {
-            TAILQ_FOREACH(bp, &env->breakpoints, entry) {
+        if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
+            QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
                 if (bp->pc == dc->pc) {
                     gen_set_condexec(dc);
                     gen_set_pc_im(dc->pc);
diff --git a/tcg/tcg-runtime.c b/tcg/tcg-runtime.c
deleted file mode 100644
index 1d77c37..0000000
--- a/tcg/tcg-runtime.c
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Tiny Code Generator for QEMU
- *
- * Copyright (c) 2008 Fabrice Bellard
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-#include <stdarg.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <inttypes.h>
-
-#include "config.h"
-#include "osdep.h"
-#include "cpu.h" // For TARGET_LONG_BITS
-#include "tcg.h"
-
-int64_t tcg_helper_shl_i64(int64_t arg1, int64_t arg2)
-{
-    return arg1 << arg2;
-}
-
-int64_t tcg_helper_shr_i64(int64_t arg1, int64_t arg2)
-{
-    return (uint64_t)arg1 >> arg2;
-}
-
-int64_t tcg_helper_sar_i64(int64_t arg1, int64_t arg2)
-{
-    return arg1 >> arg2;
-}
-
-int64_t tcg_helper_div_i64(int64_t arg1, int64_t arg2)
-{
-    return arg1 / arg2;
-}
-
-int64_t tcg_helper_rem_i64(int64_t arg1, int64_t arg2)
-{
-    return arg1 % arg2;
-}
-
-uint64_t tcg_helper_divu_i64(uint64_t arg1, uint64_t arg2)
-{
-    return arg1 / arg2;
-}
-
-uint64_t tcg_helper_remu_i64(uint64_t arg1, uint64_t arg2)
-{
-    return arg1 % arg2;
-}
diff --git a/tcg/tcg-runtime.h b/tcg/tcg-runtime.h
new file mode 100644
index 0000000..e750cc1
--- /dev/null
+++ b/tcg/tcg-runtime.h
@@ -0,0 +1,13 @@
+#ifndef TCG_RUNTIME_H
+#define TCG_RUNTIME_H
+
+/* tcg-runtime.c */
+int64_t tcg_helper_shl_i64(int64_t arg1, int64_t arg2);
+int64_t tcg_helper_shr_i64(int64_t arg1, int64_t arg2);
+int64_t tcg_helper_sar_i64(int64_t arg1, int64_t arg2);
+int64_t tcg_helper_div_i64(int64_t arg1, int64_t arg2);
+int64_t tcg_helper_rem_i64(int64_t arg1, int64_t arg2);
+uint64_t tcg_helper_divu_i64(uint64_t arg1, uint64_t arg2);
+uint64_t tcg_helper_remu_i64(uint64_t arg1, uint64_t arg2);
+
+#endif
diff --git a/vl-android.c b/vl-android.c
index 008ee7a..ad89651 100644
--- a/vl-android.c
+++ b/vl-android.c
@@ -3662,10 +3662,10 @@
 struct vm_change_state_entry {
     VMChangeStateHandler *cb;
     void *opaque;
-    LIST_ENTRY (vm_change_state_entry) entries;
+    QLIST_ENTRY (vm_change_state_entry) entries;
 };
 
-static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
+static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
 
 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
                                                      void *opaque)
@@ -3676,13 +3676,13 @@
 
     e->cb = cb;
     e->opaque = opaque;
-    LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
+    QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
     return e;
 }
 
 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
 {
-    LIST_REMOVE (e, entries);
+    QLIST_REMOVE (e, entries);
     qemu_free (e);
 }
 
@@ -5125,7 +5125,7 @@
 
     qemu_cache_utils_init(envp);
 
-    LIST_INIT (&vm_change_state_head);
+    QLIST_INIT (&vm_change_state_head);
 #ifndef _WIN32
     {
         struct sigaction act;
diff --git a/vl.c b/vl.c
index 19f5b52..aa118ff 100644
--- a/vl.c
+++ b/vl.c
@@ -3533,10 +3533,10 @@
 struct vm_change_state_entry {
     VMChangeStateHandler *cb;
     void *opaque;
-    LIST_ENTRY (vm_change_state_entry) entries;
+    QLIST_ENTRY (vm_change_state_entry) entries;
 };
 
-static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
+static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
 
 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
                                                      void *opaque)
@@ -3547,13 +3547,13 @@
 
     e->cb = cb;
     e->opaque = opaque;
-    LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
+    QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
     return e;
 }
 
 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
 {
-    LIST_REMOVE (e, entries);
+    QLIST_REMOVE (e, entries);
     qemu_free (e);
 }
 
@@ -4984,7 +4984,7 @@
 
     qemu_cache_utils_init(envp);
 
-    LIST_INIT (&vm_change_state_head);
+    QLIST_INIT (&vm_change_state_head);
 #ifndef _WIN32
     {
         struct sigaction act;