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 ZoneLow VAR32VISIBLE, ZoneHigh VAR32VISIBLE;
34 struct zone_s ZoneFSeg VAR32VISIBLE;
35 struct zone_s ZoneTmpLow VAR32VISIBLE, ZoneTmpHigh VAR32VISIBLE;
37 struct zone_s *Zones[] VAR32VISIBLE = {
38 &ZoneTmpLow, &ZoneLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
41 // Obtain memory from a given zone.
43 zone_malloc(struct zone_s *zone, u32 size, u32 align)
45 u32 newpos = (GET_PMMVAR(zone->cur) - size) / align * align;
46 if ((s32)(newpos - GET_PMMVAR(zone->bottom)) < 0)
49 SET_PMMVAR(zone->cur, newpos);
53 // Return memory to a zone (if it was the last to be allocated).
55 zone_free(struct zone_s *zone, void *data, u32 olddata)
57 if (! data || GET_PMMVAR(zone->cur) != (u32)data)
59 SET_PMMVAR(zone->cur, olddata);
62 // Find the zone that contains the given data block.
63 static struct zone_s *
67 for (i=0; i<ARRAY_SIZE(Zones); i++) {
68 struct zone_s *zone = GET_PMMVAR(Zones[i]);
69 if ((u32)data >= GET_PMMVAR(zone->cur)
70 && (u32)data < GET_PMMVAR(zone->top))
76 // Report the status of all the zones.
81 for (i=0; i<ARRAY_SIZE(Zones); i++) {
82 struct zone_s *zone = Zones[i];
83 u32 used = zone->top - zone->cur;
84 u32 avail = zone->top - zone->bottom;
85 u32 pct = avail ? ((100 * used) / avail) : 0;
86 dprintf(2, "zone %d: %08x-%08x used=%d (%d%%)\n"
87 , i, zone->bottom, zone->top, used, pct);
91 // Allocate memory at the top of 32bit ram.
95 return zone_malloc(&ZoneHigh, size, MALLOC_MIN_ALIGN);
98 // Allocate memory in the 0xf0000-0x100000 area of ram.
100 malloc_fseg(u32 size)
102 return zone_malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
109 dprintf(3, "malloc setup\n");
111 // Memory in 0xf0000 area.
112 memset(BiosTableSpace, 0, CONFIG_MAX_BIOSTABLE);
113 ZoneFSeg.bottom = (u32)BiosTableSpace;
114 ZoneFSeg.top = ZoneFSeg.cur = ZoneFSeg.bottom + CONFIG_MAX_BIOSTABLE;
116 // Memory under 1Meg.
117 ZoneTmpLow.bottom = BUILD_STACK_ADDR;
118 ZoneTmpLow.top = ZoneTmpLow.cur = (u32)MAKE_FLATPTR(EBDA_SEGMENT_MINIMUM, 0);
120 // Permanent memory under 1Meg. XXX - not implemented yet.
121 ZoneLow.bottom = ZoneLow.top = ZoneLow.cur = 0xa0000;
123 // Find memory at the top of ram.
124 struct e820entry *e = find_high_area(CONFIG_MAX_HIGHTABLE+MALLOC_MIN_ALIGN);
126 // No memory above 1Meg
127 memset(&ZoneHigh, 0, sizeof(ZoneHigh));
128 memset(&ZoneTmpHigh, 0, sizeof(ZoneTmpHigh));
131 u32 top = e->start + e->size, bottom = e->start;
133 // Memory at top of ram.
134 ZoneHigh.bottom = ALIGN(top - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
135 ZoneHigh.top = ZoneHigh.cur = ZoneHigh.bottom + CONFIG_MAX_HIGHTABLE;
136 add_e820(ZoneHigh.bottom, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
139 ZoneTmpHigh.bottom = ALIGN(bottom, MALLOC_MIN_ALIGN);
140 ZoneTmpHigh.top = ZoneTmpHigh.cur = ZoneHigh.bottom;
146 dprintf(3, "malloc finalize\n");
150 // Give back unused high ram.
151 u32 giveback = (ZoneHigh.cur - ZoneHigh.bottom) / 4096 * 4096;
152 add_e820(ZoneHigh.bottom, giveback, E820_RAM);
153 dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
155 // Clear low-memory allocations.
156 memset((void*)ZoneTmpLow.bottom, 0, ZoneTmpLow.top - ZoneTmpLow.bottom);
160 /****************************************************************
162 ****************************************************************/
164 // Information on PMM tracked allocations
170 struct pmmalloc_s *next;
173 struct pmmalloc_s *PMMAllocs VAR32VISIBLE;
175 // Memory zone that pmm allocation tracking info is stored in
176 #define ZONEALLOC (&ZoneTmpHigh)
178 // Allocate memory from the given zone and track it as a PMM allocation
180 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
182 u32 oldallocdata = GET_PMMVAR(ZONEALLOC->cur);
183 struct pmmalloc_s *info = zone_malloc(ZONEALLOC, sizeof(*info)
187 u32 olddata = GET_PMMVAR(zone->cur);
188 void *data = zone_malloc(zone, size, align);
190 zone_free(ZONEALLOC, info, oldallocdata);
193 dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
194 " ret=%p (info=%p)\n"
195 , zone, handle, size, align
197 SET_PMMVAR(info->data, data);
198 SET_PMMVAR(info->olddata, olddata);
199 SET_PMMVAR(info->handle, handle);
200 SET_PMMVAR(info->oldallocdata, oldallocdata);
201 SET_PMMVAR(info->next, GET_PMMVAR(PMMAllocs));
202 SET_PMMVAR(PMMAllocs, info);
206 // Free a raw data block (either from a zone or from pmm alloc list).
208 pmm_free_data(struct zone_s *zone, void *data, u32 olddata)
210 if (GET_PMMVAR(zone->cur) == (u32)data) {
211 zone_free(zone, data, olddata);
214 struct pmmalloc_s *info;
215 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
216 if (GET_PMMVAR(info->olddata) == (u32)data) {
217 SET_PMMVAR(info->olddata, olddata);
219 } else if (GET_PMMVAR(info->oldallocdata) == (u32)data) {
220 SET_PMMVAR(info->oldallocdata, olddata);
225 // Free a data block allocated with pmm_malloc
229 struct zone_s *zone = zone_find(GET_PMMVAR(data));
232 struct pmmalloc_s **pinfo = &PMMAllocs;
234 struct pmmalloc_s *info = GET_PMMVAR(*pinfo);
237 if (GET_PMMVAR(info->data) == data) {
238 SET_PMMVAR(*pinfo, GET_PMMVAR(info->next));
239 u32 oldallocdata = GET_PMMVAR(info->oldallocdata);
240 u32 olddata = GET_PMMVAR(info->olddata);
241 pmm_free_data(zone, data, olddata);
242 pmm_free_data(ZONEALLOC, info, oldallocdata);
243 dprintf(8, "pmm_free data=%p zone=%p olddata=%p oldallocdata=%p"
245 , data, zone, (void*)olddata, (void*)oldallocdata
253 // Find the amount of free space in a given zone.
255 pmm_getspace(struct zone_s *zone)
257 u32 space = GET_PMMVAR(zone->cur) - GET_PMMVAR(zone->bottom);
258 if (zone != ZONEALLOC)
260 u32 reserve = ALIGN(sizeof(struct pmmalloc_s), MALLOC_MIN_ALIGN);
261 if (space <= reserve)
263 return space - reserve;
266 // Find the data block allocated with pmm_malloc with a given handle.
270 struct pmmalloc_s *info;
271 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
272 if (GET_PMMVAR(info->handle) == handle)
273 return GET_PMMVAR(info->data);
278 /****************************************************************
280 ****************************************************************/
292 extern struct pmmheader PMMHEADER;
294 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
297 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
299 .length = sizeof(PMMHEADER),
300 .entry_seg = SEG_BIOS,
304 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
308 handle_pmm00(u16 *args)
310 u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
312 dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
313 , length, handle, flags);
314 struct zone_s *lowzone = &ZoneTmpLow, *highzone = &ZoneTmpHigh;
316 // Permanent memory request.
318 highzone = &ZoneHigh;
321 // Memory size request
327 return pmm_getspace(lowzone);
329 return pmm_getspace(highzone);
331 u32 spacelow = pmm_getspace(lowzone);
332 u32 spacehigh = pmm_getspace(highzone);
333 if (spacelow > spacehigh)
339 u32 size = length * 16;
342 u32 align = MALLOC_MIN_ALIGN;
344 align = 1<<__ffs(size);
345 if (align < MALLOC_MIN_ALIGN)
346 align = MALLOC_MIN_ALIGN;
353 return (u32)pmm_malloc(lowzone, handle, size, align);
355 return (u32)pmm_malloc(highzone, handle, size, align);
357 void *data = pmm_malloc(lowzone, handle, size, align);
360 return (u32)pmm_malloc(highzone, handle, size, align);
367 handle_pmm01(u16 *args)
369 u32 handle = *(u32*)&args[1];
370 dprintf(3, "pmm01: handle=%x\n", handle);
371 if (handle == 0xFFFFFFFF)
373 return (u32)pmm_find(handle);
378 handle_pmm02(u16 *args)
380 u32 buffer = *(u32*)&args[1];
381 dprintf(3, "pmm02: buffer=%x\n", buffer);
382 int ret = pmm_free((void*)buffer);
390 handle_pmmXX(u16 *args)
392 return PMM_FUNCTION_NOT_SUPPORTED;
396 handle_pmm(u16 *args)
399 return PMM_FUNCTION_NOT_SUPPORTED;
402 dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
405 case 0x00: return handle_pmm00(args);
406 case 0x01: return handle_pmm01(args);
407 case 0x02: return handle_pmm02(args);
408 default: return handle_pmmXX(args);
413 extern void entry_pmm();
421 dprintf(3, "init PMM\n");
425 PMMHEADER.signature = PMM_SIGNATURE;
426 PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
427 PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
436 dprintf(3, "finalize PMM\n");
438 PMMHEADER.signature = 0;
439 PMMHEADER.entry_offset = 0;