grml...
[seabios.git] / src / pmm.c
index b3eef4bc314a725661f0094651c20dadd4c43321..82a0b1d0dc4fb61ff58605fe5683d904cb5323d0 100644 (file)
--- a/src/pmm.c
+++ b/src/pmm.c
 
 #include "util.h" // checksum
 #include "config.h" // BUILD_BIOS_ADDR
-#include "memmap.h" // e820_list
+#include "memmap.h" // struct e820entry
 #include "farptr.h" // GET_FARVAR
-#include "biosvar.h" // EBDA_SEGMENT_MINIMUM
+#include "biosvar.h" // GET_BDA
 
+// Information on a reserved area.
+struct allocinfo_s {
+    struct allocinfo_s *next, **pprev;
+    void *data, *dataend, *allocend;
+};
 
-/****************************************************************
- * malloc
- ****************************************************************/
-
-#if MODE16
-// The 16bit pmm entry points runs in "big real" mode, and can
-// therefore read/write to the 32bit malloc variables.
-#define GET_PMMVAR(var) GET_FARVAR(0, (var))
-#define SET_PMMVAR(var, val) SET_FARVAR(0, (var), (val))
-#else
-#define GET_PMMVAR(var) (var)
-#define SET_PMMVAR(var, val) do { (var) = (val); } while (0)
-#endif
+// Information on a tracked memory allocation.
+struct allocdetail_s {
+    struct allocinfo_s detailinfo;
+    struct allocinfo_s datainfo;
+    u32 handle;
+};
 
-// Zone definitions
+// The various memory zones.
 struct zone_s {
-    u32 top, bottom, cur;
+    struct allocinfo_s *info;
 };
 
-struct zone_s ZoneHigh VAR32VISIBLE, ZoneFSeg VAR32VISIBLE;
-struct zone_s ZoneTmpLow VAR32VISIBLE, ZoneTmpHigh VAR32VISIBLE;
+struct zone_s ZoneLow, ZoneHigh, ZoneFSeg, ZoneTmpLow, ZoneTmpHigh;
 
-struct zone_s *Zones[] VAR32VISIBLE = {
-    &ZoneTmpLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
+static struct zone_s *Zones[] = {
+    &ZoneTmpLow, &ZoneLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
 };
 
-// Obtain memory from a given zone.
+
+/****************************************************************
+ * low-level memory reservations
+ ****************************************************************/
+
+// Find and reserve space from a given zone
 static void *
-zone_malloc(struct zone_s *zone, u32 size, u32 align)
+allocSpace(struct zone_s *zone, u32 size, u32 align, struct allocinfo_s *fill)
 {
-    u32 newpos = (GET_PMMVAR(zone->cur) - size) / align * align;
-    if ((s32)(newpos - GET_PMMVAR(zone->bottom)) < 0)
-        // No space
-        return NULL;
-    SET_PMMVAR(zone->cur, newpos);
-    return (void*)newpos;
+    struct allocinfo_s *info;
+    for (info = zone->info; info; info = info->next) {
+        void *dataend = info->dataend;
+        void *allocend = info->allocend;
+        void *newallocend = (void*)ALIGN_DOWN((u32)allocend - size, align);
+        if (newallocend >= dataend && newallocend <= allocend) {
+            // Found space - now reserve it.
+            struct allocinfo_s **pprev = info->pprev;
+            if (!fill)
+                fill = newallocend;
+            fill->next = info;
+            fill->pprev = pprev;
+            fill->data = newallocend;
+            fill->dataend = newallocend + size;
+            fill->allocend = allocend;
+
+            info->allocend = newallocend;
+            info->pprev = &fill->next;
+            *pprev = fill;
+            return newallocend;
+        }
+    }
+    return NULL;
 }
 
-// Return memory to a zone (if it was the last to be allocated).
+// Release space allocated with allocSpace()
 static void
-zone_free(struct zone_s *zone, void *data, u32 olddata)
+freeSpace(struct allocinfo_s *info)
 {
-    if (! data || GET_PMMVAR(zone->cur) != (u32)data)
-        return;
-    SET_PMMVAR(zone->cur, olddata);
+    struct allocinfo_s *next = info->next;
+    struct allocinfo_s **pprev = info->pprev;
+    *pprev = next;
+    if (next) {
+        if (next->allocend == info->data)
+            next->allocend = info->allocend;
+        next->pprev = pprev;
+    }
 }
 
-// Find the zone that contains the given data block.
-static struct zone_s *
-zone_find(void *data)
+// Add new memory to a zone
+static void
+addSpace(struct zone_s *zone, void *start, void *end)
 {
-    int i;
-    for (i=0; i<ARRAY_SIZE(Zones); i++) {
-        struct zone_s *zone = GET_PMMVAR(Zones[i]);
-        if ((u32)data >= GET_PMMVAR(zone->cur)
-            && (u32)data < GET_PMMVAR(zone->top))
-            return zone;
+    // Find position to add space
+    struct allocinfo_s **pprev = &zone->info, *info;
+    for (;;) {
+        info = *pprev;
+        if (!info || info->data < start)
+            break;
+        pprev = &info->next;
     }
-    return NULL;
+
+    // Add space using temporary allocation info.
+    struct allocdetail_s tempdetail;
+    tempdetail.datainfo.next = info;
+    tempdetail.datainfo.pprev = pprev;
+    tempdetail.datainfo.data = tempdetail.datainfo.dataend = start;
+    tempdetail.datainfo.allocend = end;
+    *pprev = &tempdetail.datainfo;
+    if (info)
+        info->pprev = &tempdetail.datainfo.next;
+
+    // Allocate final allocation info.
+    struct allocdetail_s *detail = allocSpace(
+        &ZoneTmpHigh, sizeof(*detail), MALLOC_MIN_ALIGN, NULL);
+    if (!detail) {
+        detail = allocSpace(&ZoneTmpLow, sizeof(*detail)
+                            , MALLOC_MIN_ALIGN, NULL);
+        if (!detail) {
+            *tempdetail.datainfo.pprev = tempdetail.datainfo.next;
+            if (tempdetail.datainfo.next)
+                tempdetail.datainfo.next->pprev = tempdetail.datainfo.pprev;
+            warn_noalloc();
+            return;
+        }
+    }
+
+    // Replace temp alloc space with final alloc space
+    memcpy(&detail->datainfo, &tempdetail.datainfo, sizeof(detail->datainfo));
+    detail->handle = PMM_DEFAULT_HANDLE;
+
+    *tempdetail.datainfo.pprev = &detail->datainfo;
+    if (tempdetail.datainfo.next)
+        tempdetail.datainfo.next->pprev = &detail->datainfo.next;
 }
 
-// Report the status of all the zones.
-static void
-dumpZones()
+// Search all zones for an allocation obtained from allocSpace()
+static struct allocinfo_s *
+findAlloc(void *data)
 {
     int i;
     for (i=0; i<ARRAY_SIZE(Zones); i++) {
         struct zone_s *zone = Zones[i];
-        u32 used = zone->top - zone->cur;
-        u32 avail = zone->top - zone->bottom;
-        u32 pct = avail ? ((100 * used) / avail) : 0;
-        dprintf(2, "zone %d: %08x-%08x used=%d (%d%%)\n"
-                , i, zone->bottom, zone->top, used, pct);
+        struct allocinfo_s *info;
+        for (info = zone->info; info; info = info->next)
+            if (info->data == data)
+                return info;
     }
+    return NULL;
 }
 
-// Allocate memory at the top of 32bit ram.
-void *
-malloc_high(u32 size)
+// Return the last sentinal node of a zone
+static struct allocinfo_s *
+findLast(struct zone_s *zone)
 {
-    return zone_malloc(&ZoneHigh, size, MALLOC_MIN_ALIGN);
+    struct allocinfo_s *info = zone->info;
+    if (!info)
+        return NULL;
+    for (;;) {
+        struct allocinfo_s *next = info->next;
+        if (!next)
+            return info;
+        info = next;
+    }
 }
 
-// Allocate memory in the 0xf0000-0x100000 area of ram.
-void *
-malloc_fseg(u32 size)
-{
-    return zone_malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
-}
+
+/****************************************************************
+ * Setup
+ ****************************************************************/
 
 void
-malloc_setup()
+malloc_setup(void)
 {
-    ASSERT32();
+    ASSERT32FLAT();
     dprintf(3, "malloc setup\n");
 
-    // Memory in 0xf0000 area.
-    memset(BiosTableSpace, 0, CONFIG_MAX_BIOSTABLE);
-    ZoneFSeg.bottom = (u32)BiosTableSpace;
-    ZoneFSeg.top = ZoneFSeg.cur = ZoneFSeg.bottom + CONFIG_MAX_BIOSTABLE;
-
-    // Memory under 1Meg.
-    ZoneTmpLow.bottom = BUILD_STACK_ADDR;
-    ZoneTmpLow.top = ZoneTmpLow.cur = (u32)MAKE_FLATPTR(EBDA_SEGMENT_MINIMUM, 0);
-
-    // Find memory at the top of ram.
-    u32 top = 0, bottom = 0;
+    // Populate temp high ram
+    u32 highram = 0;
     int i;
     for (i=e820_count-1; i>=0; i--) {
-        struct e820entry *e = &e820_list[i];
-        u64 end = e->start + e->size;
-        if (e->type != E820_RAM || end > 0xffffffff
-            || e->size < CONFIG_MAX_HIGHTABLE + MALLOC_MIN_ALIGN)
+        struct e820entry *en = &e820_list[i];
+        u64 end = en->start + en->size;
+        if (end < 1024*1024)
+            break;
+        if (en->type != E820_RAM || end > 0xffffffff)
             continue;
-        top = end;
-        bottom = e->start;
-        break;
+        u32 s = en->start, e = end;
+        if (!highram) {
+            u32 newe = ALIGN_DOWN(e - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
+            if (newe <= e && newe >= s) {
+                highram = newe;
+                e = newe;
+            }
+        }
+        addSpace(&ZoneTmpHigh, (void*)s, (void*)e);
     }
-    if (top < 1024*1024 + CONFIG_MAX_HIGHTABLE) {
-        // No memory above 1Meg
-        memset(&ZoneHigh, 0, sizeof(ZoneHigh));
-        memset(&ZoneTmpHigh, 0, sizeof(ZoneHigh));
-        return;
+
+    // Populate other regions
+    addSpace(&ZoneTmpLow, (void*)BUILD_STACK_ADDR, (void*)BUILD_EBDA_MINIMUM);
+    addSpace(&ZoneFSeg, BiosTableSpace, &BiosTableSpace[CONFIG_MAX_BIOSTABLE]);
+    addSpace(&ZoneLow, (void*)BUILD_LOWRAM_END, (void*)BUILD_LOWRAM_END);
+    if (highram) {
+        addSpace(&ZoneHigh, (void*)highram
+                 , (void*)highram + CONFIG_MAX_HIGHTABLE);
+        add_e820(highram, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
     }
+}
+
+// Update pointers after code relocation.
+void
+malloc_fixupreloc(void)
+{
+    ASSERT32FLAT();
+    if (!CONFIG_RELOCATE_INIT)
+        return;
+    dprintf(3, "malloc fixup reloc\n");
 
-    // Memory at top of ram.
-    ZoneHigh.bottom = ALIGN(top - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
-    ZoneHigh.top = ZoneHigh.cur = ZoneHigh.bottom + CONFIG_MAX_HIGHTABLE;
-    add_e820(ZoneHigh.bottom, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
+    int i;
+    for (i=0; i<ARRAY_SIZE(Zones); i++) {
+        struct zone_s *zone = Zones[i];
+        zone->info->pprev = &zone->info;
+    }
 
-    // Memory above 1Meg
-    ZoneTmpHigh.bottom = ALIGN(bottom, MALLOC_MIN_ALIGN);
-    ZoneTmpHigh.top = ZoneTmpHigh.cur = ZoneHigh.bottom;
+    // Add space free'd during relocation in f-segment to ZoneFSeg
+    extern u8 code32init_end[];
+    if ((u32)code32init_end > BUILD_BIOS_ADDR) {
+        memset((void*)BUILD_BIOS_ADDR, 0, (u32)code32init_end - BUILD_BIOS_ADDR);
+        addSpace(&ZoneFSeg, (void*)BUILD_BIOS_ADDR, code32init_end);
+    }
 }
 
 void
-malloc_finalize()
+malloc_finalize(void)
 {
+    ASSERT32FLAT();
     dprintf(3, "malloc finalize\n");
 
-    dumpZones();
+    // Reserve more low-mem if needed.
+    u32 endlow = GET_BDA(mem_size_kb)*1024;
+    add_e820(endlow, BUILD_LOWRAM_END-endlow, E820_RESERVED);
 
     // Give back unused high ram.
-    u32 giveback = (ZoneHigh.cur - ZoneHigh.bottom) / 4096 * 4096;
-    add_e820(ZoneHigh.bottom, giveback, E820_RAM);
-    dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
-
-    // Clear low-memory allocations.
-    memset((void*)ZoneTmpLow.bottom, 0, ZoneTmpLow.top - ZoneTmpLow.bottom);
+    struct allocinfo_s *info = findLast(&ZoneHigh);
+    if (info) {
+        u32 giveback = ALIGN_DOWN(info->allocend - info->dataend, PAGE_SIZE);
+        add_e820((u32)info->dataend, giveback, E820_RAM);
+        dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
+    }
 }
 
 
 /****************************************************************
- * pmm allocation
+ * ebda movement
  ****************************************************************/
 
-// Information on PMM tracked allocations
-struct pmmalloc_s {
-    void *data;
-    u32 olddata;
-    u32 handle;
-    u32 oldallocdata;
-    struct pmmalloc_s *next;
-};
+// Move ebda
+static int
+relocate_ebda(u32 newebda, u32 oldebda, u8 ebda_size)
+{
+    u32 lowram = GET_BDA(mem_size_kb) * 1024;
+    if (oldebda != lowram)
+        // EBDA isn't at end of ram - give up.
+        return -1;
 
-struct pmmalloc_s *PMMAllocs VAR32VISIBLE;
+    // Do copy
+    memmove((void*)newebda, (void*)oldebda, ebda_size * 1024);
 
-// Memory zone that pmm allocation tracking info is stored in
-#define ZONEALLOC (&ZoneTmpHigh)
+    // Update indexes
+    dprintf(1, "ebda moved from %x to %x\n", oldebda, newebda);
+    SET_BDA(mem_size_kb, newebda / 1024);
+    SET_BDA(ebda_seg, FLATPTR_TO_SEG(newebda));
+    return 0;
+}
 
-// Allocate memory from the given zone and track it as a PMM allocation
+// Support expanding the ZoneLow dynamically.
+static void
+zonelow_expand(u32 size, u32 align)
+{
+    struct allocinfo_s *info = findLast(&ZoneLow);
+    if (!info)
+        return;
+    u32 oldpos = (u32)info->allocend;
+    u32 newpos = ALIGN_DOWN(oldpos - size, align);
+    u32 bottom = (u32)info->dataend;
+    if (newpos >= bottom && newpos <= oldpos)
+        // Space already present.
+        return;
+    u16 ebda_seg = get_ebda_seg();
+    u32 ebda_pos = (u32)MAKE_FLATPTR(ebda_seg, 0);
+    u8 ebda_size = GET_EBDA2(ebda_seg, size);
+    u32 ebda_end = ebda_pos + ebda_size * 1024;
+    if (ebda_end != bottom)
+        // Something else is after ebda - can't use any existing space.
+        newpos = ALIGN_DOWN(ebda_end - size, align);
+    u32 newbottom = ALIGN_DOWN(newpos, 1024);
+    u32 newebda = ALIGN_DOWN(newbottom - ebda_size * 1024, 1024);
+    if (newebda < BUILD_EBDA_MINIMUM)
+        // Not enough space.
+        return;
+
+    // Move ebda
+    int ret = relocate_ebda(newebda, ebda_pos, ebda_size);
+    if (ret)
+        return;
+
+    // Update zone
+    if (ebda_end == bottom) {
+        info->data = (void*)newbottom;
+        info->dataend = (void*)newbottom;
+    } else
+        addSpace(&ZoneLow, (void*)newbottom, (void*)ebda_end);
+}
+
+// Check if can expand the given zone to fulfill an allocation
 static void *
+allocExpandSpace(struct zone_s *zone, u32 size, u32 align
+                 , struct allocinfo_s *fill)
+{
+    void *data = allocSpace(zone, size, align, fill);
+    if (data || zone != &ZoneLow)
+        return data;
+
+    // Make sure to not move ebda while an optionrom is running.
+    if (unlikely(wait_preempt())) {
+        data = allocSpace(zone, size, align, fill);
+        if (data)
+            return data;
+    }
+
+    zonelow_expand(size, align);
+    return allocSpace(zone, size, align, fill);
+}
+
+
+/****************************************************************
+ * tracked memory allocations
+ ****************************************************************/
+
+// Allocate memory from the given zone and track it as a PMM allocation
+void * __malloc
 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
 {
-    u32 oldallocdata = GET_PMMVAR(ZONEALLOC->cur);
-    struct pmmalloc_s *info = zone_malloc(ZONEALLOC, sizeof(*info)
-                                          , MALLOC_MIN_ALIGN);
-    if (!info)
+    ASSERT32FLAT();
+    if (!size)
         return NULL;
-    u32 olddata = GET_PMMVAR(zone->cur);
-    void *data = zone_malloc(zone, size, align);
-    if (! data) {
-        zone_free(ZONEALLOC, info, oldallocdata);
+
+    // Find and reserve space for bookkeeping.
+    struct allocdetail_s *detail = allocSpace(
+        &ZoneTmpHigh, sizeof(*detail), MALLOC_MIN_ALIGN, NULL);
+    if (!detail) {
+        detail = allocSpace(&ZoneTmpLow, sizeof(*detail)
+                            , MALLOC_MIN_ALIGN, NULL);
+        if (!detail)
+            return NULL;
+    }
+
+    // Find and reserve space for main allocation
+    void *data = allocExpandSpace(zone, size, align, &detail->datainfo);
+    if (!data) {
+        freeSpace(&detail->detailinfo);
         return NULL;
     }
+
     dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
-            " ret=%p (info=%p)\n"
+            " ret=%p (detail=%p)\n"
             , zone, handle, size, align
-            , data, info);
-    SET_PMMVAR(info->data, data);
-    SET_PMMVAR(info->olddata, olddata);
-    SET_PMMVAR(info->handle, handle);
-    SET_PMMVAR(info->oldallocdata, oldallocdata);
-    SET_PMMVAR(info->next, GET_PMMVAR(PMMAllocs));
-    SET_PMMVAR(PMMAllocs, info);
-    return data;
-}
+            , data, detail);
+    detail->handle = handle;
 
-// Free a raw data block (either from a zone or from pmm alloc list).
-static void
-pmm_free_data(struct zone_s *zone, void *data, u32 olddata)
-{
-    if (GET_PMMVAR(zone->cur) == (u32)data) {
-        zone_free(zone, data, olddata);
-        return;
-    }
-    struct pmmalloc_s *info;
-    for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
-        if (GET_PMMVAR(info->olddata) == (u32)data) {
-            SET_PMMVAR(info->olddata, olddata);
-            return;
-        } else if (GET_PMMVAR(info->oldallocdata) == (u32)data) {
-            SET_PMMVAR(info->oldallocdata, olddata);
-            return;
-        }
+    return data;
 }
 
 // Free a data block allocated with pmm_malloc
-static int
+int
 pmm_free(void *data)
 {
-    struct zone_s *zone = zone_find(GET_PMMVAR(data));
-    if (!zone)
+    ASSERT32FLAT();
+    struct allocinfo_s *info = findAlloc(data);
+    if (!info || data == (void*)info || data == info->dataend)
         return -1;
-    struct pmmalloc_s **pinfo = &PMMAllocs;
-    for (;;) {
-        struct pmmalloc_s *info = GET_PMMVAR(*pinfo);
-        if (!info)
-            return -1;
-        if (GET_PMMVAR(info->data) == data) {
-            SET_PMMVAR(*pinfo, GET_PMMVAR(info->next));
-            u32 oldallocdata = GET_PMMVAR(info->oldallocdata);
-            u32 olddata = GET_PMMVAR(info->olddata);
-            pmm_free_data(zone, data, olddata);
-            pmm_free_data(ZONEALLOC, info, oldallocdata);
-            dprintf(8, "pmm_free data=%p zone=%p olddata=%p oldallocdata=%p"
-                    " info=%p\n"
-                    , data, zone, (void*)olddata, (void*)oldallocdata
-                    , info);
-            return 0;
-        }
-        pinfo = &info->next;
-    }
+    struct allocdetail_s *detail = container_of(
+        info, struct allocdetail_s, datainfo);
+    dprintf(8, "pmm_free %p (detail=%p)\n", data, detail);
+    freeSpace(info);
+    freeSpace(&detail->detailinfo);
+    return 0;
 }
 
 // Find the amount of free space in a given zone.
 static u32
 pmm_getspace(struct zone_s *zone)
 {
-    u32 space = GET_PMMVAR(zone->cur) - GET_PMMVAR(zone->bottom);
-    if (zone != ZONEALLOC)
-        return space;
-    u32 reserve = ALIGN(sizeof(struct pmmalloc_s), MALLOC_MIN_ALIGN);
-    if (space <= reserve)
+    // XXX - doesn't account for ZoneLow being able to grow.
+    // XXX - results not reliable when CONFIG_THREAD_OPTIONROMS
+    u32 maxspace = 0;
+    struct allocinfo_s *info;
+    for (info = zone->info; info; info = info->next) {
+        u32 space = info->allocend - info->dataend;
+        if (space > maxspace)
+            maxspace = space;
+    }
+
+    if (zone != &ZoneTmpHigh && zone != &ZoneTmpLow)
+        return maxspace;
+    // Account for space needed for PMM tracking.
+    u32 reserve = ALIGN(sizeof(struct allocdetail_s), MALLOC_MIN_ALIGN);
+    if (maxspace <= reserve)
         return 0;
-    return space - reserve;
+    return maxspace - reserve;
 }
 
 // Find the data block allocated with pmm_malloc with a given handle.
 static void *
 pmm_find(u32 handle)
 {
-    struct pmmalloc_s *info;
-    for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
-        if (GET_PMMVAR(info->handle) == handle)
-            return GET_PMMVAR(info->data);
+    int i;
+    for (i=0; i<ARRAY_SIZE(Zones); i++) {
+        struct zone_s *zone = Zones[i];
+        struct allocinfo_s *info;
+        for (info = zone->info; info; info = info->next) {
+            if (info->data != (void*)info)
+                continue;
+            struct allocdetail_s *detail = container_of(
+                info, struct allocdetail_s, detailinfo);
+            if (detail->handle == handle)
+                return detail->datainfo.data;
+        }
+    }
     return NULL;
 }
 
@@ -317,6 +462,12 @@ handle_pmm00(u16 *args)
     u16 flags = args[5];
     dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
             , length, handle, flags);
+    struct zone_s *lowzone = &ZoneTmpLow, *highzone = &ZoneTmpHigh;
+    if (flags & 8) {
+        // Permanent memory request.
+        lowzone = &ZoneLow;
+        highzone = &ZoneHigh;
+    }
     if (!length) {
         // Memory size request
         switch (flags & 3) {
@@ -324,12 +475,12 @@ handle_pmm00(u16 *args)
         case 0:
             return 0;
         case 1:
-            return pmm_getspace(&ZoneTmpLow);
+            return pmm_getspace(lowzone);
         case 2:
-            return pmm_getspace(&ZoneTmpHigh);
+            return pmm_getspace(highzone);
         case 3: {
-            u32 spacelow = pmm_getspace(&ZoneTmpLow);
-            u32 spacehigh = pmm_getspace(&ZoneTmpHigh);
+            u32 spacelow = pmm_getspace(lowzone);
+            u32 spacehigh = pmm_getspace(highzone);
             if (spacelow > spacehigh)
                 return spacelow;
             return spacehigh;
@@ -350,14 +501,14 @@ handle_pmm00(u16 *args)
     case 0:
         return 0;
     case 1:
-        return (u32)pmm_malloc(&ZoneTmpLow, handle, size, align);
+        return (u32)pmm_malloc(lowzone, handle, size, align);
     case 2:
-        return (u32)pmm_malloc(&ZoneTmpHigh, handle, size, align);
+        return (u32)pmm_malloc(highzone, handle, size, align);
     case 3: {
-        void *data = pmm_malloc(&ZoneTmpLow, handle, size, align);
+        void *data = pmm_malloc(lowzone, handle, size, align);
         if (data)
             return (u32)data;
-        return (u32)pmm_malloc(&ZoneTmpHigh, handle, size, align);
+        return (u32)pmm_malloc(highzone, handle, size, align);
     }
     }
 }
@@ -368,7 +519,7 @@ handle_pmm01(u16 *args)
 {
     u32 handle = *(u32*)&args[1];
     dprintf(3, "pmm01: handle=%x\n", handle);
-    if (handle == 0xFFFFFFFF)
+    if (handle == PMM_DEFAULT_HANDLE)
         return 0;
     return (u32)pmm_find(handle);
 }
@@ -392,43 +543,55 @@ handle_pmmXX(u16 *args)
     return PMM_FUNCTION_NOT_SUPPORTED;
 }
 
-u32 VISIBLE16
+u32 VISIBLE32INIT
 handle_pmm(u16 *args)
 {
+    ASSERT32FLAT();
     if (! CONFIG_PMM)
         return PMM_FUNCTION_NOT_SUPPORTED;
 
     u16 arg1 = args[0];
     dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
 
+    int oldpreempt;
+    if (CONFIG_THREAD_OPTIONROMS) {
+        // Not a preemption event - don't wait in wait_preempt()
+        oldpreempt = CanPreempt;
+        CanPreempt = 0;
+    }
+
+    u32 ret;
     switch (arg1) {
-    case 0x00: return handle_pmm00(args);
-    case 0x01: return handle_pmm01(args);
-    case 0x02: return handle_pmm02(args);
-    default:   return handle_pmmXX(args);
+    case 0x00: ret = handle_pmm00(args); break;
+    case 0x01: ret = handle_pmm01(args); break;
+    case 0x02: ret = handle_pmm02(args); break;
+    default:   ret = handle_pmmXX(args); break;
     }
+
+    if (CONFIG_THREAD_OPTIONROMS)
+        CanPreempt = oldpreempt;
+
+    return ret;
 }
 
 // romlayout.S
-extern void entry_pmm();
+extern void entry_pmm(void);
 
 void
-pmm_setup()
+pmm_setup(void)
 {
     if (! CONFIG_PMM)
         return;
 
     dprintf(3, "init PMM\n");
 
-    PMMAllocs = NULL;
-
     PMMHEADER.signature = PMM_SIGNATURE;
     PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
     PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
 }
 
 void
-pmm_finalize()
+pmm_finalize(void)
 {
     if (! CONFIG_PMM)
         return;