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" // e820_list
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 dprintf(2, "zone %d: %08x-%08x used=%d (%d%%)\n"
85 , i, zone->bottom, zone->top, used, (100 * used) / avail);
89 // Allocate memory at the top of 32bit ram.
93 return zone_malloc(&ZoneHigh, size, MALLOC_MIN_ALIGN);
96 // Allocate memory in the 0xf0000-0x100000 area of ram.
100 return zone_malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
107 dprintf(3, "malloc setup\n");
109 // Memory in 0xf0000 area.
110 memset(BiosTableSpace, 0, CONFIG_MAX_BIOSTABLE);
111 ZoneFSeg.bottom = (u32)BiosTableSpace;
112 ZoneFSeg.top = ZoneFSeg.cur = ZoneFSeg.bottom + CONFIG_MAX_BIOSTABLE;
114 // Memory under 1Meg.
115 ZoneTmpLow.bottom = BUILD_STACK_ADDR;
116 ZoneTmpLow.top = ZoneTmpLow.cur = (u32)MAKE_FLATPTR(EBDA_SEGMENT_MINIMUM, 0);
118 // Find memory at the top of ram.
119 u32 top = 0, bottom = 0;
121 for (i=e820_count-1; i>=0; i--) {
122 struct e820entry *e = &e820_list[i];
123 u64 end = e->start + e->size;
124 if (e->type != E820_RAM || end > 0xffffffff
125 || e->size < CONFIG_MAX_HIGHTABLE)
131 if (top < 1024*1024 + CONFIG_MAX_HIGHTABLE) {
132 // No memory above 1Meg
133 memset(&ZoneHigh, 0, sizeof(ZoneHigh));
137 // Memory at top of ram.
138 ZoneHigh.bottom = top - CONFIG_MAX_HIGHTABLE;
139 ZoneHigh.top = ZoneHigh.cur = ZoneHigh.bottom + CONFIG_MAX_HIGHTABLE;
140 add_e820(ZoneHigh.bottom, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
143 ZoneTmpHigh.bottom = bottom;
144 ZoneTmpHigh.top = ZoneTmpHigh.cur = ZoneHigh.bottom;
150 dprintf(3, "malloc finalize\n");
154 // Give back unused high ram.
155 u32 giveback = (ZoneHigh.cur - ZoneHigh.bottom) / 4096 * 4096;
156 add_e820(ZoneHigh.bottom, giveback, E820_RAM);
157 dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
159 // Clear low-memory allocations.
160 memset((void*)ZoneTmpLow.bottom, 0, ZoneTmpLow.top - ZoneTmpLow.bottom);
164 /****************************************************************
166 ****************************************************************/
168 // Information on PMM tracked allocations
173 struct pmmalloc_s *next;
176 struct pmmalloc_s *PMMAllocs VAR32VISIBLE;
178 #define PMMALLOCSIZE ALIGN(sizeof(struct pmmalloc_s), MALLOC_MIN_ALIGN)
180 // Allocate memory from the given zone and track it as a PMM allocation
182 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
184 struct pmmalloc_s *info = zone_malloc(&ZoneTmpHigh, sizeof(*info)
188 u32 olddata = GET_PMMVAR(zone->cur);
189 void *data = zone_malloc(zone, size, align);
191 zone_free(&ZoneTmpHigh, info, (u32)info + PMMALLOCSIZE);
194 dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
195 " ret=%p (info=%p)\n"
196 , zone, handle, size, align
198 SET_PMMVAR(info->data, data);
199 SET_PMMVAR(info->olddata, olddata);
200 SET_PMMVAR(info->handle, handle);
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);
222 // Free a data block allocated with pmm_malloc
226 struct zone_s *zone = zone_find(GET_PMMVAR(data));
229 struct pmmalloc_s **pinfo = &PMMAllocs;
231 struct pmmalloc_s *info = GET_PMMVAR(*pinfo);
234 if (GET_PMMVAR(info->data) == data) {
235 SET_PMMVAR(*pinfo, GET_PMMVAR(info->next));
236 u32 olddata = GET_PMMVAR(info->olddata);
237 pmm_free_data(zone, data, olddata);
238 pmm_free_data(&ZoneTmpHigh, info, (u32)info + PMMALLOCSIZE);
239 dprintf(8, "pmm_free data=%p zone=%p olddata=%p info=%p\n"
240 , data, zone, (void*)olddata, info);
248 // Find the amount of free space in a given zone.
250 pmm_getspace(struct zone_s *zone)
252 u32 space = GET_PMMVAR(zone->cur) - GET_PMMVAR(zone->bottom);
253 if (space <= PMMALLOCSIZE)
255 return space - PMMALLOCSIZE;
258 // Find the data block allocated with pmm_malloc with a given handle.
262 struct pmmalloc_s *info;
263 for (info=GET_PMMVAR(PMMAllocs); info; info = GET_PMMVAR(info->next))
264 if (GET_PMMVAR(info->handle) == handle)
265 return GET_PMMVAR(info->data);
270 /****************************************************************
272 ****************************************************************/
284 extern struct pmmheader PMMHEADER;
286 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
289 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
291 .length = sizeof(PMMHEADER),
292 .entry_seg = SEG_BIOS,
296 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
300 handle_pmm00(u16 *args)
302 u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
304 dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
305 , length, handle, flags);
307 // Memory size request
313 return pmm_getspace(&ZoneTmpLow);
315 return pmm_getspace(&ZoneTmpHigh);
317 u32 spacelow = pmm_getspace(&ZoneTmpLow);
318 u32 spacehigh = pmm_getspace(&ZoneTmpHigh);
319 if (spacelow > spacehigh)
325 u32 size = length * 16;
328 u32 align = MALLOC_MIN_ALIGN;
330 align = 1<<__ffs(size);
331 if (align < MALLOC_MIN_ALIGN)
332 align = MALLOC_MIN_ALIGN;
339 return (u32)pmm_malloc(&ZoneTmpLow, handle, size, align);
341 return (u32)pmm_malloc(&ZoneTmpHigh, handle, size, align);
343 void *data = pmm_malloc(&ZoneTmpLow, handle, size, align);
346 return (u32)pmm_malloc(&ZoneTmpHigh, handle, size, align);
353 handle_pmm01(u16 *args)
355 u32 handle = *(u32*)&args[1];
356 dprintf(3, "pmm01: handle=%x\n", handle);
357 if (handle == 0xFFFFFFFF)
359 return (u32)pmm_find(handle);
364 handle_pmm02(u16 *args)
366 u32 buffer = *(u32*)&args[1];
367 dprintf(3, "pmm02: buffer=%x\n", buffer);
368 int ret = pmm_free((void*)buffer);
376 handle_pmmXX(u16 *args)
378 return PMM_FUNCTION_NOT_SUPPORTED;
382 handle_pmm(u16 *args)
385 return PMM_FUNCTION_NOT_SUPPORTED;
388 dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
391 case 0x00: return handle_pmm00(args);
392 case 0x01: return handle_pmm01(args);
393 case 0x02: return handle_pmm02(args);
394 default: return handle_pmmXX(args);
399 extern void entry_pmm();
407 dprintf(3, "init PMM\n");
409 PMMHEADER.signature = PMM_SIGNATURE;
410 PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
411 PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
420 dprintf(3, "finalize PMM\n");
422 PMMHEADER.signature = 0;
423 PMMHEADER.entry_offset = 0;