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 u32 oldpos = GET_PMMVAR(ZoneLow.cur);
73 u32 newpos = ALIGN_DOWN(oldpos - size, align);
74 u32 bottom = GET_PMMVAR(ZoneLow.bottom);
75 if (newpos >= bottom && newpos <= oldpos)
76 // Space already present.
78 u16 ebda_seg = get_ebda_seg();
79 u32 ebda_pos = (u32)MAKE_FLATPTR(ebda_seg, 0);
80 u8 ebda_size = GET_EBDA2(ebda_seg, size);
81 u32 ebda_end = ebda_pos + ebda_size * 1024;
82 if (ebda_end != bottom) {
83 // Something else is after ebda - can't use any existing space.
85 newpos = ALIGN_DOWN(oldpos - size, align);
87 u32 newbottom = ALIGN_DOWN(newpos, 1024);
88 u32 newebda = ALIGN_DOWN(newbottom - ebda_size * 1024, 1024);
89 if (newebda < BUILD_EBDA_MINIMUM)
94 int ret = relocate_ebda(newebda, ebda_pos, ebda_size);
99 SET_PMMVAR(ZoneLow.cur, oldpos);
100 SET_PMMVAR(ZoneLow.bottom, newbottom);
104 /****************************************************************
106 ****************************************************************/
108 // Obtain memory from a given zone.
110 zone_malloc(struct zone_s *zone, u32 size, u32 align)
112 u32 oldpos = GET_PMMVAR(zone->cur);
113 u32 newpos = ALIGN_DOWN(oldpos - size, align);
114 if (newpos < GET_PMMVAR(zone->bottom) || newpos > oldpos)
117 SET_PMMVAR(zone->cur, newpos);
118 return (void*)newpos;
121 // Find the zone that contains the given data block.
122 static struct zone_s *
123 zone_find(void *data)
126 for (i=0; i<ARRAY_SIZE(Zones); i++) {
127 struct zone_s *zone = GET_PMMVAR(Zones[i]);
128 if ((u32)data >= GET_PMMVAR(zone->cur)
129 && (u32)data < GET_PMMVAR(zone->top))
135 // Return memory to a zone (if it was the last to be allocated).
137 zone_free(void *data, u32 olddata)
139 struct zone_s *zone = zone_find(data);
140 if (!zone || !data || GET_PMMVAR(zone->cur) != (u32)data)
142 SET_PMMVAR(zone->cur, olddata);
146 // Report the status of all the zones.
151 for (i=0; i<ARRAY_SIZE(Zones); i++) {
152 struct zone_s *zone = Zones[i];
153 u32 used = zone->top - zone->cur;
154 u32 avail = zone->top - zone->bottom;
155 u32 pct = avail ? ((100 * used) / avail) : 0;
156 dprintf(2, "zone %d: %08x-%08x used=%d (%d%%)\n"
157 , i, zone->bottom, zone->top, used, pct);
162 /****************************************************************
163 * tracked memory allocations
164 ****************************************************************/
166 // Information on PMM tracked allocations
172 struct pmmalloc_s *next;
175 struct pmmalloc_s *PMMAllocs VAR32VISIBLE;
177 // Allocate memory from the given zone and track it as a PMM allocation
179 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
181 u32 oldallocdata = GET_PMMVAR(ZoneTmpHigh.cur);
182 struct pmmalloc_s *info = zone_malloc(&ZoneTmpHigh, sizeof(*info)
185 oldallocdata = GET_PMMVAR(ZoneTmpLow.cur);
186 info = zone_malloc(&ZoneTmpLow, sizeof(*info), MALLOC_MIN_ALIGN);
190 if (zone == &ZoneLow)
191 zonelow_expand(size, align);
192 u32 olddata = GET_PMMVAR(zone->cur);
193 void *data = zone_malloc(zone, size, align);
195 zone_free(info, oldallocdata);
198 dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
199 " ret=%p (info=%p)\n"
200 , zone, handle, size, align
202 SET_PMMVAR(info->data, data);
203 SET_PMMVAR(info->olddata, olddata);
204 SET_PMMVAR(info->handle, handle);
205 SET_PMMVAR(info->oldallocdata, oldallocdata);
206 SET_PMMVAR(info->next, GET_PMMVAR(PMMAllocs));
207 SET_PMMVAR(PMMAllocs, info);
211 // Free a raw data block (either from a zone or from pmm alloc list).
213 pmm_free_data(void *data, u32 olddata)
215 int ret = zone_free(data, olddata);
219 struct pmmalloc_s *info;
220 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
221 if (GET_PMMVAR(info->olddata) == (u32)data) {
222 SET_PMMVAR(info->olddata, olddata);
224 } else if (GET_PMMVAR(info->oldallocdata) == (u32)data) {
225 SET_PMMVAR(info->oldallocdata, olddata);
230 // Free a data block allocated with pmm_malloc
234 struct pmmalloc_s **pinfo = &PMMAllocs;
236 struct pmmalloc_s *info = GET_PMMVAR(*pinfo);
239 if (GET_PMMVAR(info->data) == data) {
240 SET_PMMVAR(*pinfo, GET_PMMVAR(info->next));
241 u32 oldallocdata = GET_PMMVAR(info->oldallocdata);
242 u32 olddata = GET_PMMVAR(info->olddata);
243 pmm_free_data(data, olddata);
244 pmm_free_data(info, oldallocdata);
245 dprintf(8, "pmm_free data=%p olddata=%p oldallocdata=%p info=%p\n"
246 , data, (void*)olddata, (void*)oldallocdata, info);
253 // Find the amount of free space in a given zone.
255 pmm_getspace(struct zone_s *zone)
257 // XXX - doesn't account for ZoneLow being able to grow.
258 u32 space = GET_PMMVAR(zone->cur) - GET_PMMVAR(zone->bottom);
259 if (zone != &ZoneTmpHigh && zone != &ZoneTmpLow)
261 // Account for space needed for PMM tracking.
262 u32 reserve = ALIGN(sizeof(struct pmmalloc_s), MALLOC_MIN_ALIGN);
263 if (space <= reserve)
265 return space - reserve;
268 // Find the data block allocated with pmm_malloc with a given handle.
272 struct pmmalloc_s *info;
273 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
274 if (GET_PMMVAR(info->handle) == handle)
275 return GET_PMMVAR(info->data);
283 dprintf(3, "malloc setup\n");
287 // Memory in 0xf0000 area.
288 memset(BiosTableSpace, 0, CONFIG_MAX_BIOSTABLE);
289 ZoneFSeg.bottom = (u32)BiosTableSpace;
290 ZoneFSeg.top = ZoneFSeg.cur = ZoneFSeg.bottom + CONFIG_MAX_BIOSTABLE;
292 // Memory under 1Meg.
293 ZoneTmpLow.bottom = BUILD_STACK_ADDR;
294 ZoneTmpLow.top = ZoneTmpLow.cur = BUILD_EBDA_MINIMUM;
296 // Permanent memory under 1Meg.
297 ZoneLow.bottom = ZoneLow.top = ZoneLow.cur = BUILD_LOWRAM_END;
299 // Find memory at the top of ram.
300 struct e820entry *e = find_high_area(CONFIG_MAX_HIGHTABLE+MALLOC_MIN_ALIGN);
302 // No memory above 1Meg
303 memset(&ZoneHigh, 0, sizeof(ZoneHigh));
304 memset(&ZoneTmpHigh, 0, sizeof(ZoneTmpHigh));
307 u32 top = e->start + e->size, bottom = e->start;
309 // Memory at top of ram.
310 ZoneHigh.bottom = ALIGN(top - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
311 ZoneHigh.top = ZoneHigh.cur = ZoneHigh.bottom + CONFIG_MAX_HIGHTABLE;
312 add_e820(ZoneHigh.bottom, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
315 ZoneTmpHigh.bottom = ALIGN(bottom, MALLOC_MIN_ALIGN);
316 ZoneTmpHigh.top = ZoneTmpHigh.cur = ZoneHigh.bottom;
322 dprintf(3, "malloc finalize\n");
326 // Reserve more low-mem if needed.
327 u32 endlow = GET_BDA(mem_size_kb)*1024;
328 add_e820(endlow, BUILD_LOWRAM_END-endlow, E820_RESERVED);
330 // Give back unused high ram.
331 u32 giveback = ALIGN_DOWN(ZoneHigh.cur - ZoneHigh.bottom, PAGE_SIZE);
332 add_e820(ZoneHigh.bottom, giveback, E820_RAM);
333 dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
335 // Clear low-memory allocations.
336 memset((void*)ZoneTmpLow.bottom, 0, ZoneTmpLow.top - ZoneTmpLow.bottom);
340 /****************************************************************
342 ****************************************************************/
354 extern struct pmmheader PMMHEADER;
356 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
359 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
361 .length = sizeof(PMMHEADER),
362 .entry_seg = SEG_BIOS,
366 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
370 handle_pmm00(u16 *args)
372 u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
374 dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
375 , length, handle, flags);
376 struct zone_s *lowzone = &ZoneTmpLow, *highzone = &ZoneTmpHigh;
378 // Permanent memory request.
380 highzone = &ZoneHigh;
383 // Memory size request
389 return pmm_getspace(lowzone);
391 return pmm_getspace(highzone);
393 u32 spacelow = pmm_getspace(lowzone);
394 u32 spacehigh = pmm_getspace(highzone);
395 if (spacelow > spacehigh)
401 u32 size = length * 16;
404 u32 align = MALLOC_MIN_ALIGN;
406 align = 1<<__ffs(size);
407 if (align < MALLOC_MIN_ALIGN)
408 align = MALLOC_MIN_ALIGN;
415 return (u32)pmm_malloc(lowzone, handle, size, align);
417 return (u32)pmm_malloc(highzone, handle, size, align);
419 void *data = pmm_malloc(lowzone, handle, size, align);
422 return (u32)pmm_malloc(highzone, handle, size, align);
429 handle_pmm01(u16 *args)
431 u32 handle = *(u32*)&args[1];
432 dprintf(3, "pmm01: handle=%x\n", handle);
433 if (handle == PMM_DEFAULT_HANDLE)
435 return (u32)pmm_find(handle);
440 handle_pmm02(u16 *args)
442 u32 buffer = *(u32*)&args[1];
443 dprintf(3, "pmm02: buffer=%x\n", buffer);
444 int ret = pmm_free((void*)buffer);
452 handle_pmmXX(u16 *args)
454 return PMM_FUNCTION_NOT_SUPPORTED;
458 handle_pmm(u16 *args)
461 return PMM_FUNCTION_NOT_SUPPORTED;
464 dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
467 case 0x00: return handle_pmm00(args);
468 case 0x01: return handle_pmm01(args);
469 case 0x02: return handle_pmm02(args);
470 default: return handle_pmmXX(args);
475 extern void entry_pmm();
483 dprintf(3, "init PMM\n");
485 PMMHEADER.signature = PMM_SIGNATURE;
486 PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
487 PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
496 dprintf(3, "finalize PMM\n");
498 PMMHEADER.signature = 0;
499 PMMHEADER.entry_offset = 0;