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" // GET_BDA
15 // The 16bit pmm entry points runs in "big real" mode, and can
16 // therefore read/write to the 32bit malloc variables.
17 #define GET_PMMVAR(var) GET_FARVAR(0, (var))
18 #define SET_PMMVAR(var, val) SET_FARVAR(0, (var), (val))
20 #define GET_PMMVAR(var) (var)
21 #define SET_PMMVAR(var, val) do { (var) = (val); } while (0)
29 struct zone_s ZoneLow VAR32VISIBLE, ZoneHigh VAR32VISIBLE;
30 struct zone_s ZoneFSeg VAR32VISIBLE;
31 struct zone_s ZoneTmpLow VAR32VISIBLE, ZoneTmpHigh VAR32VISIBLE;
33 struct zone_s *Zones[] VAR32VISIBLE = {
34 &ZoneTmpLow, &ZoneLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
38 /****************************************************************
40 ****************************************************************/
44 relocate_ebda(u32 newebda, u32 oldebda, u8 ebda_size)
46 u32 lowram = GET_BDA(mem_size_kb) * 1024;
47 if (oldebda != lowram)
48 // EBDA isn't at end of ram - give up.
53 memcpy_far(FLATPTR_TO_SEG(newebda)
54 , (void*)FLATPTR_TO_OFFSET(newebda)
55 , FLATPTR_TO_SEG(oldebda)
56 , (void*)FLATPTR_TO_OFFSET(oldebda)
59 memmove((void*)newebda, (void*)oldebda, ebda_size * 1024);
62 dprintf(1, "ebda moved from %x to %x\n", oldebda, newebda);
63 SET_BDA(mem_size_kb, newebda / 1024);
64 SET_BDA(ebda_seg, FLATPTR_TO_SEG(newebda));
68 // Support expanding the ZoneLow dynamically.
70 zonelow_expand(u32 size, u32 align)
72 u16 ebda_seg = get_ebda_seg();
73 u32 ebda_pos = (u32)MAKE_FLATPTR(ebda_seg, 0);
74 u32 bottom = GET_PMMVAR(ZoneLow.bottom);
75 u32 cur = GET_PMMVAR(ZoneLow.cur);
76 u8 ebda_size = GET_EBDA2(ebda_seg, size);
77 u32 ebda_end = ebda_pos + ebda_size * 1024;
78 if (ebda_end != bottom)
79 // Something else is after ebda - can't use any existing space.
81 u32 newcur = ALIGN_DOWN(cur - size, align);
82 u32 newbottom = ALIGN_DOWN(newcur, 1024);
83 u32 newebda = ALIGN_DOWN(newbottom - ebda_size * 1024, 1024);
84 if (newebda < BUILD_EBDA_MINIMUM)
89 int ret = relocate_ebda(newebda, ebda_pos, ebda_size);
94 SET_PMMVAR(ZoneLow.bottom, newbottom);
99 /****************************************************************
101 ****************************************************************/
103 // Obtain memory from a given zone.
105 zone_malloc(struct zone_s *zone, u32 size, u32 align)
107 u32 oldpos = GET_PMMVAR(zone->cur);
108 u32 newpos = ALIGN_DOWN(oldpos - size, align);
109 if (newpos < GET_PMMVAR(zone->bottom) || newpos > oldpos)
112 SET_PMMVAR(zone->cur, newpos);
113 return (void*)newpos;
116 // Return memory to a zone (if it was the last to be allocated).
118 zone_free(struct zone_s *zone, void *data, u32 olddata)
120 if (! data || GET_PMMVAR(zone->cur) != (u32)data)
122 SET_PMMVAR(zone->cur, olddata);
125 // Find the zone that contains the given data block.
126 static struct zone_s *
127 zone_find(void *data)
130 for (i=0; i<ARRAY_SIZE(Zones); i++) {
131 struct zone_s *zone = GET_PMMVAR(Zones[i]);
132 if ((u32)data >= GET_PMMVAR(zone->cur)
133 && (u32)data < GET_PMMVAR(zone->top))
139 // Report the status of all the zones.
144 for (i=0; i<ARRAY_SIZE(Zones); i++) {
145 struct zone_s *zone = Zones[i];
146 u32 used = zone->top - zone->cur;
147 u32 avail = zone->top - zone->bottom;
148 u32 pct = avail ? ((100 * used) / avail) : 0;
149 dprintf(2, "zone %d: %08x-%08x used=%d (%d%%)\n"
150 , i, zone->bottom, zone->top, used, pct);
158 dprintf(3, "malloc setup\n");
160 // Memory in 0xf0000 area.
161 memset(BiosTableSpace, 0, CONFIG_MAX_BIOSTABLE);
162 ZoneFSeg.bottom = (u32)BiosTableSpace;
163 ZoneFSeg.top = ZoneFSeg.cur = ZoneFSeg.bottom + CONFIG_MAX_BIOSTABLE;
165 // Memory under 1Meg.
166 ZoneTmpLow.bottom = BUILD_STACK_ADDR;
167 ZoneTmpLow.top = ZoneTmpLow.cur = BUILD_EBDA_MINIMUM;
169 // Permanent memory under 1Meg.
170 ZoneLow.bottom = ZoneLow.top = ZoneLow.cur = BUILD_LOWRAM_END;
172 // Find memory at the top of ram.
173 struct e820entry *e = find_high_area(CONFIG_MAX_HIGHTABLE+MALLOC_MIN_ALIGN);
175 // No memory above 1Meg
176 memset(&ZoneHigh, 0, sizeof(ZoneHigh));
177 memset(&ZoneTmpHigh, 0, sizeof(ZoneTmpHigh));
180 u32 top = e->start + e->size, bottom = e->start;
182 // Memory at top of ram.
183 ZoneHigh.bottom = ALIGN(top - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
184 ZoneHigh.top = ZoneHigh.cur = ZoneHigh.bottom + CONFIG_MAX_HIGHTABLE;
185 add_e820(ZoneHigh.bottom, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
188 ZoneTmpHigh.bottom = ALIGN(bottom, MALLOC_MIN_ALIGN);
189 ZoneTmpHigh.top = ZoneTmpHigh.cur = ZoneHigh.bottom;
195 dprintf(3, "malloc finalize\n");
199 // Reserve more low-mem if needed.
200 u32 endlow = GET_BDA(mem_size_kb)*1024;
201 add_e820(endlow, BUILD_LOWRAM_END-endlow, E820_RESERVED);
203 // Give back unused high ram.
204 u32 giveback = ALIGN_DOWN(ZoneHigh.cur - ZoneHigh.bottom, PAGE_SIZE);
205 add_e820(ZoneHigh.bottom, giveback, E820_RAM);
206 dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
208 // Clear low-memory allocations.
209 memset((void*)ZoneTmpLow.bottom, 0, ZoneTmpLow.top - ZoneTmpLow.bottom);
212 // Allocate memory from ZoneLow - growing the zone if needed.
214 zone_malloc_low(u32 size, u32 align)
216 void *ret = zone_malloc(&ZoneLow, size, align);
219 zonelow_expand(size, align);
220 return zone_malloc(&ZoneLow, size, align);
224 /****************************************************************
226 ****************************************************************/
228 // Information on PMM tracked allocations
234 struct pmmalloc_s *next;
237 struct pmmalloc_s *PMMAllocs VAR32VISIBLE;
239 // Memory zone that pmm allocation tracking info is stored in
240 #define ZONEALLOC (&ZoneTmpHigh)
242 // Allocate memory from the given zone and track it as a PMM allocation
244 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
246 u32 oldallocdata = GET_PMMVAR(ZONEALLOC->cur);
247 struct pmmalloc_s *info = zone_malloc(ZONEALLOC, sizeof(*info)
251 u32 olddata = GET_PMMVAR(zone->cur);
252 void *data = zone_malloc(zone, size, align);
253 if (!data && zone == &ZoneLow) {
254 // Try to expand permanent low zone.
255 zonelow_expand(size, align);
256 olddata = GET_PMMVAR(zone->cur);
257 data = zone_malloc(zone, size, align);
260 zone_free(ZONEALLOC, info, oldallocdata);
263 dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
264 " ret=%p (info=%p)\n"
265 , zone, handle, size, align
267 SET_PMMVAR(info->data, data);
268 SET_PMMVAR(info->olddata, olddata);
269 SET_PMMVAR(info->handle, handle);
270 SET_PMMVAR(info->oldallocdata, oldallocdata);
271 SET_PMMVAR(info->next, GET_PMMVAR(PMMAllocs));
272 SET_PMMVAR(PMMAllocs, info);
276 // Free a raw data block (either from a zone or from pmm alloc list).
278 pmm_free_data(struct zone_s *zone, void *data, u32 olddata)
280 if (GET_PMMVAR(zone->cur) == (u32)data) {
281 zone_free(zone, data, olddata);
284 struct pmmalloc_s *info;
285 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
286 if (GET_PMMVAR(info->olddata) == (u32)data) {
287 SET_PMMVAR(info->olddata, olddata);
289 } else if (GET_PMMVAR(info->oldallocdata) == (u32)data) {
290 SET_PMMVAR(info->oldallocdata, olddata);
295 // Free a data block allocated with pmm_malloc
299 struct zone_s *zone = zone_find(GET_PMMVAR(data));
302 struct pmmalloc_s **pinfo = &PMMAllocs;
304 struct pmmalloc_s *info = GET_PMMVAR(*pinfo);
307 if (GET_PMMVAR(info->data) == data) {
308 SET_PMMVAR(*pinfo, GET_PMMVAR(info->next));
309 u32 oldallocdata = GET_PMMVAR(info->oldallocdata);
310 u32 olddata = GET_PMMVAR(info->olddata);
311 pmm_free_data(zone, data, olddata);
312 pmm_free_data(ZONEALLOC, info, oldallocdata);
313 dprintf(8, "pmm_free data=%p zone=%p olddata=%p oldallocdata=%p"
315 , data, zone, (void*)olddata, (void*)oldallocdata
323 // Find the amount of free space in a given zone.
325 pmm_getspace(struct zone_s *zone)
327 // XXX - doesn't account for ZoneLow being able to grow.
328 u32 space = GET_PMMVAR(zone->cur) - GET_PMMVAR(zone->bottom);
329 if (zone != ZONEALLOC)
331 u32 reserve = ALIGN(sizeof(struct pmmalloc_s), MALLOC_MIN_ALIGN);
332 if (space <= reserve)
334 return space - reserve;
337 // Find the data block allocated with pmm_malloc with a given handle.
341 struct pmmalloc_s *info;
342 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
343 if (GET_PMMVAR(info->handle) == handle)
344 return GET_PMMVAR(info->data);
349 /****************************************************************
351 ****************************************************************/
363 extern struct pmmheader PMMHEADER;
365 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
368 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
370 .length = sizeof(PMMHEADER),
371 .entry_seg = SEG_BIOS,
375 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
379 handle_pmm00(u16 *args)
381 u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
383 dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
384 , length, handle, flags);
385 struct zone_s *lowzone = &ZoneTmpLow, *highzone = &ZoneTmpHigh;
387 // Permanent memory request.
389 highzone = &ZoneHigh;
392 // Memory size request
398 return pmm_getspace(lowzone);
400 return pmm_getspace(highzone);
402 u32 spacelow = pmm_getspace(lowzone);
403 u32 spacehigh = pmm_getspace(highzone);
404 if (spacelow > spacehigh)
410 u32 size = length * 16;
413 u32 align = MALLOC_MIN_ALIGN;
415 align = 1<<__ffs(size);
416 if (align < MALLOC_MIN_ALIGN)
417 align = MALLOC_MIN_ALIGN;
424 return (u32)pmm_malloc(lowzone, handle, size, align);
426 return (u32)pmm_malloc(highzone, handle, size, align);
428 void *data = pmm_malloc(lowzone, handle, size, align);
431 return (u32)pmm_malloc(highzone, handle, size, align);
438 handle_pmm01(u16 *args)
440 u32 handle = *(u32*)&args[1];
441 dprintf(3, "pmm01: handle=%x\n", handle);
442 if (handle == 0xFFFFFFFF)
444 return (u32)pmm_find(handle);
449 handle_pmm02(u16 *args)
451 u32 buffer = *(u32*)&args[1];
452 dprintf(3, "pmm02: buffer=%x\n", buffer);
453 int ret = pmm_free((void*)buffer);
461 handle_pmmXX(u16 *args)
463 return PMM_FUNCTION_NOT_SUPPORTED;
467 handle_pmm(u16 *args)
470 return PMM_FUNCTION_NOT_SUPPORTED;
473 dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
476 case 0x00: return handle_pmm00(args);
477 case 0x01: return handle_pmm01(args);
478 case 0x02: return handle_pmm02(args);
479 default: return handle_pmmXX(args);
484 extern void entry_pmm();
492 dprintf(3, "init PMM\n");
496 PMMHEADER.signature = PMM_SIGNATURE;
497 PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
498 PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
507 dprintf(3, "finalize PMM\n");
509 PMMHEADER.signature = 0;
510 PMMHEADER.entry_offset = 0;