1 // Post memory manager (PMM) calls
3 // Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 #include "util.h" // checksum
8 #include "config.h" // BUILD_BIOS_ADDR
9 #include "memmap.h" // find_high_area
10 #include "farptr.h" // GET_FARVAR
11 #include "biosvar.h" // EBDA_SEGMENT_MINIMUM
14 /****************************************************************
16 ****************************************************************/
19 // The 16bit pmm entry points runs in "big real" mode, and can
20 // therefore read/write to the 32bit malloc variables.
21 #define GET_PMMVAR(var) GET_FARVAR(0, (var))
22 #define SET_PMMVAR(var, val) SET_FARVAR(0, (var), (val))
24 #define GET_PMMVAR(var) (var)
25 #define SET_PMMVAR(var, val) do { (var) = (val); } while (0)
33 struct zone_s ZoneHigh VAR32VISIBLE, ZoneFSeg VAR32VISIBLE;
34 struct zone_s ZoneTmpLow VAR32VISIBLE, ZoneTmpHigh VAR32VISIBLE;
36 struct zone_s *Zones[] VAR32VISIBLE = {
37 &ZoneTmpLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
40 // Obtain memory from a given zone.
42 zone_malloc(struct zone_s *zone, u32 size, u32 align)
44 u32 newpos = (GET_PMMVAR(zone->cur) - size) / align * align;
45 if ((s32)(newpos - GET_PMMVAR(zone->bottom)) < 0)
48 SET_PMMVAR(zone->cur, newpos);
52 // Return memory to a zone (if it was the last to be allocated).
54 zone_free(struct zone_s *zone, void *data, u32 olddata)
56 if (! data || GET_PMMVAR(zone->cur) != (u32)data)
58 SET_PMMVAR(zone->cur, olddata);
61 // Find the zone that contains the given data block.
62 static struct zone_s *
66 for (i=0; i<ARRAY_SIZE(Zones); i++) {
67 struct zone_s *zone = GET_PMMVAR(Zones[i]);
68 if ((u32)data >= GET_PMMVAR(zone->cur)
69 && (u32)data < GET_PMMVAR(zone->top))
75 // Report the status of all the zones.
80 for (i=0; i<ARRAY_SIZE(Zones); i++) {
81 struct zone_s *zone = Zones[i];
82 u32 used = zone->top - zone->cur;
83 u32 avail = zone->top - zone->bottom;
84 u32 pct = avail ? ((100 * used) / avail) : 0;
85 dprintf(2, "zone %d: %08x-%08x used=%d (%d%%)\n"
86 , i, zone->bottom, zone->top, used, pct);
90 // Allocate memory at the top of 32bit ram.
94 return zone_malloc(&ZoneHigh, size, MALLOC_MIN_ALIGN);
97 // Allocate memory in the 0xf0000-0x100000 area of ram.
101 return zone_malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
108 dprintf(3, "malloc setup\n");
110 // Memory in 0xf0000 area.
111 memset(BiosTableSpace, 0, CONFIG_MAX_BIOSTABLE);
112 ZoneFSeg.bottom = (u32)BiosTableSpace;
113 ZoneFSeg.top = ZoneFSeg.cur = ZoneFSeg.bottom + CONFIG_MAX_BIOSTABLE;
115 // Memory under 1Meg.
116 ZoneTmpLow.bottom = BUILD_STACK_ADDR;
117 ZoneTmpLow.top = ZoneTmpLow.cur = (u32)MAKE_FLATPTR(EBDA_SEGMENT_MINIMUM, 0);
119 // Find memory at the top of ram.
120 struct e820entry *e = find_high_area(CONFIG_MAX_HIGHTABLE+MALLOC_MIN_ALIGN);
122 // No memory above 1Meg
123 memset(&ZoneHigh, 0, sizeof(ZoneHigh));
124 memset(&ZoneTmpHigh, 0, sizeof(ZoneTmpHigh));
127 u32 top = e->start + e->size, bottom = e->start;
129 // Memory at top of ram.
130 ZoneHigh.bottom = ALIGN(top - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
131 ZoneHigh.top = ZoneHigh.cur = ZoneHigh.bottom + CONFIG_MAX_HIGHTABLE;
132 add_e820(ZoneHigh.bottom, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
135 ZoneTmpHigh.bottom = ALIGN(bottom, MALLOC_MIN_ALIGN);
136 ZoneTmpHigh.top = ZoneTmpHigh.cur = ZoneHigh.bottom;
142 dprintf(3, "malloc finalize\n");
146 // Give back unused high ram.
147 u32 giveback = (ZoneHigh.cur - ZoneHigh.bottom) / 4096 * 4096;
148 add_e820(ZoneHigh.bottom, giveback, E820_RAM);
149 dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
151 // Clear low-memory allocations.
152 memset((void*)ZoneTmpLow.bottom, 0, ZoneTmpLow.top - ZoneTmpLow.bottom);
156 /****************************************************************
158 ****************************************************************/
160 // Information on PMM tracked allocations
166 struct pmmalloc_s *next;
169 struct pmmalloc_s *PMMAllocs VAR32VISIBLE;
171 // Memory zone that pmm allocation tracking info is stored in
172 #define ZONEALLOC (&ZoneTmpHigh)
174 // Allocate memory from the given zone and track it as a PMM allocation
176 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
178 u32 oldallocdata = GET_PMMVAR(ZONEALLOC->cur);
179 struct pmmalloc_s *info = zone_malloc(ZONEALLOC, sizeof(*info)
183 u32 olddata = GET_PMMVAR(zone->cur);
184 void *data = zone_malloc(zone, size, align);
186 zone_free(ZONEALLOC, info, oldallocdata);
189 dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
190 " ret=%p (info=%p)\n"
191 , zone, handle, size, align
193 SET_PMMVAR(info->data, data);
194 SET_PMMVAR(info->olddata, olddata);
195 SET_PMMVAR(info->handle, handle);
196 SET_PMMVAR(info->oldallocdata, oldallocdata);
197 SET_PMMVAR(info->next, GET_PMMVAR(PMMAllocs));
198 SET_PMMVAR(PMMAllocs, info);
202 // Free a raw data block (either from a zone or from pmm alloc list).
204 pmm_free_data(struct zone_s *zone, void *data, u32 olddata)
206 if (GET_PMMVAR(zone->cur) == (u32)data) {
207 zone_free(zone, data, olddata);
210 struct pmmalloc_s *info;
211 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
212 if (GET_PMMVAR(info->olddata) == (u32)data) {
213 SET_PMMVAR(info->olddata, olddata);
215 } else if (GET_PMMVAR(info->oldallocdata) == (u32)data) {
216 SET_PMMVAR(info->oldallocdata, olddata);
221 // Free a data block allocated with pmm_malloc
225 struct zone_s *zone = zone_find(GET_PMMVAR(data));
228 struct pmmalloc_s **pinfo = &PMMAllocs;
230 struct pmmalloc_s *info = GET_PMMVAR(*pinfo);
233 if (GET_PMMVAR(info->data) == data) {
234 SET_PMMVAR(*pinfo, GET_PMMVAR(info->next));
235 u32 oldallocdata = GET_PMMVAR(info->oldallocdata);
236 u32 olddata = GET_PMMVAR(info->olddata);
237 pmm_free_data(zone, data, olddata);
238 pmm_free_data(ZONEALLOC, info, oldallocdata);
239 dprintf(8, "pmm_free data=%p zone=%p olddata=%p oldallocdata=%p"
241 , data, zone, (void*)olddata, (void*)oldallocdata
249 // Find the amount of free space in a given zone.
251 pmm_getspace(struct zone_s *zone)
253 u32 space = GET_PMMVAR(zone->cur) - GET_PMMVAR(zone->bottom);
254 if (zone != ZONEALLOC)
256 u32 reserve = ALIGN(sizeof(struct pmmalloc_s), MALLOC_MIN_ALIGN);
257 if (space <= reserve)
259 return space - reserve;
262 // Find the data block allocated with pmm_malloc with a given handle.
266 struct pmmalloc_s *info;
267 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
268 if (GET_PMMVAR(info->handle) == handle)
269 return GET_PMMVAR(info->data);
274 /****************************************************************
276 ****************************************************************/
288 extern struct pmmheader PMMHEADER;
290 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
293 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
295 .length = sizeof(PMMHEADER),
296 .entry_seg = SEG_BIOS,
300 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
304 handle_pmm00(u16 *args)
306 u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
308 dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
309 , length, handle, flags);
311 // Memory size request
317 return pmm_getspace(&ZoneTmpLow);
319 return pmm_getspace(&ZoneTmpHigh);
321 u32 spacelow = pmm_getspace(&ZoneTmpLow);
322 u32 spacehigh = pmm_getspace(&ZoneTmpHigh);
323 if (spacelow > spacehigh)
329 u32 size = length * 16;
332 u32 align = MALLOC_MIN_ALIGN;
334 align = 1<<__ffs(size);
335 if (align < MALLOC_MIN_ALIGN)
336 align = MALLOC_MIN_ALIGN;
343 return (u32)pmm_malloc(&ZoneTmpLow, handle, size, align);
345 return (u32)pmm_malloc(&ZoneTmpHigh, handle, size, align);
347 void *data = pmm_malloc(&ZoneTmpLow, handle, size, align);
350 return (u32)pmm_malloc(&ZoneTmpHigh, handle, size, align);
357 handle_pmm01(u16 *args)
359 u32 handle = *(u32*)&args[1];
360 dprintf(3, "pmm01: handle=%x\n", handle);
361 if (handle == 0xFFFFFFFF)
363 return (u32)pmm_find(handle);
368 handle_pmm02(u16 *args)
370 u32 buffer = *(u32*)&args[1];
371 dprintf(3, "pmm02: buffer=%x\n", buffer);
372 int ret = pmm_free((void*)buffer);
380 handle_pmmXX(u16 *args)
382 return PMM_FUNCTION_NOT_SUPPORTED;
386 handle_pmm(u16 *args)
389 return PMM_FUNCTION_NOT_SUPPORTED;
392 dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
395 case 0x00: return handle_pmm00(args);
396 case 0x01: return handle_pmm01(args);
397 case 0x02: return handle_pmm02(args);
398 default: return handle_pmmXX(args);
403 extern void entry_pmm();
411 dprintf(3, "init PMM\n");
415 PMMHEADER.signature = PMM_SIGNATURE;
416 PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
417 PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
426 dprintf(3, "finalize PMM\n");
428 PMMHEADER.signature = 0;
429 PMMHEADER.entry_offset = 0;