Add Post Memory Manager (PMM) support.
[seabios.git] / src / pmm.c
1 // Post memory manager (PMM) calls
2 //
3 // Copyright (C) 2009  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6
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
12
13
14 /****************************************************************
15  * malloc
16  ****************************************************************/
17
18 #if MODE16
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))
23 #else
24 #define GET_PMMVAR(var) (var)
25 #define SET_PMMVAR(var, val) do { (var) = (val); } while (0)
26 #endif
27
28 // Zone definitions
29 struct zone_s {
30     u32 top, bottom, cur;
31 };
32
33 struct zone_s ZoneHigh VAR32VISIBLE, ZoneFSeg VAR32VISIBLE;
34 struct zone_s ZoneTmpLow VAR32VISIBLE, ZoneTmpHigh VAR32VISIBLE;
35
36 struct zone_s *Zones[] VAR32VISIBLE = {
37     &ZoneTmpLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
38 };
39
40 // Obtain memory from a given zone.
41 static void *
42 zone_malloc(struct zone_s *zone, u32 size, u32 align)
43 {
44     u32 newpos = (GET_PMMVAR(zone->cur) - size) / align * align;
45     if ((s32)(newpos - GET_PMMVAR(zone->bottom)) < 0)
46         // No space
47         return NULL;
48     SET_PMMVAR(zone->cur, newpos);
49     return (void*)newpos;
50 }
51
52 // Return memory to a zone (if it was the last to be allocated).
53 static void
54 zone_free(struct zone_s *zone, void *data, u32 olddata)
55 {
56     if (! data || GET_PMMVAR(zone->cur) != (u32)data)
57         return;
58     SET_PMMVAR(zone->cur, olddata);
59 }
60
61 // Find the zone that contains the given data block.
62 static struct zone_s *
63 zone_find(void *data)
64 {
65     int i;
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))
70             return zone;
71     }
72     return NULL;
73 }
74
75 // Report the status of all the zones.
76 static void
77 dumpZones()
78 {
79     int i;
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);
86     }
87 }
88
89 // Allocate memory at the top of 32bit ram.
90 void *
91 malloc_high(u32 size)
92 {
93     return zone_malloc(&ZoneHigh, size, MALLOC_MIN_ALIGN);
94 }
95
96 // Allocate memory in the 0xf0000-0x100000 area of ram.
97 void *
98 malloc_fseg(u32 size)
99 {
100     return zone_malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
101 }
102
103 void
104 malloc_setup()
105 {
106     ASSERT32();
107     dprintf(3, "malloc setup\n");
108
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;
113
114     // Memory under 1Meg.
115     ZoneTmpLow.bottom = BUILD_STACK_ADDR;
116     ZoneTmpLow.top = ZoneTmpLow.cur = (u32)MAKE_FLATPTR(EBDA_SEGMENT_MINIMUM, 0);
117
118     // Find memory at the top of ram.
119     u32 top = 0, bottom = 0;
120     int i;
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)
126             continue;
127         top = end;
128         bottom = e->start;
129         break;
130     }
131     if (top < 1024*1024 + CONFIG_MAX_HIGHTABLE) {
132         // No memory above 1Meg
133         memset(&ZoneHigh, 0, sizeof(ZoneHigh));
134         return;
135     }
136
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);
141
142     // Memory above 1Meg
143     ZoneTmpHigh.bottom = bottom;
144     ZoneTmpHigh.top = ZoneTmpHigh.cur = ZoneHigh.bottom;
145 }
146
147 void
148 malloc_finalize()
149 {
150     dprintf(3, "malloc finalize\n");
151
152     dumpZones();
153
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);
158
159     // Clear low-memory allocations.
160     memset((void*)ZoneTmpLow.bottom, 0, ZoneTmpLow.top - ZoneTmpLow.bottom);
161 }
162
163
164 /****************************************************************
165  * pmm allocation
166  ****************************************************************/
167
168 // Information on PMM tracked allocations
169 struct pmmalloc_s {
170     void *data;
171     u32 olddata;
172     u32 handle;
173     struct pmmalloc_s *next;
174 };
175
176 struct pmmalloc_s *PMMAllocs VAR32VISIBLE;
177
178 #define PMMALLOCSIZE ALIGN(sizeof(struct pmmalloc_s), MALLOC_MIN_ALIGN)
179
180 // Allocate memory from the given zone and track it as a PMM allocation
181 static void *
182 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
183 {
184     struct pmmalloc_s *info = zone_malloc(&ZoneTmpHigh, sizeof(*info)
185                                           , MALLOC_MIN_ALIGN);
186     if (!info)
187         return NULL;
188     u32 olddata = GET_PMMVAR(zone->cur);
189     void *data = zone_malloc(zone, size, align);
190     if (! data) {
191         zone_free(&ZoneTmpHigh, info, (u32)info + PMMALLOCSIZE);
192         return NULL;
193     }
194     dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
195             " ret=%p (info=%p)\n"
196             , zone, handle, size, align
197             , data, info);
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);
203     return data;
204 }
205
206 // Free a raw data block (either from a zone or from pmm alloc list).
207 static void
208 pmm_free_data(struct zone_s *zone, void *data, u32 olddata)
209 {
210     if (GET_PMMVAR(zone->cur) == (u32)data) {
211         zone_free(zone, data, olddata);
212         return;
213     }
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);
218             return;
219         }
220 }
221
222 // Free a data block allocated with pmm_malloc
223 static int
224 pmm_free(void *data)
225 {
226     struct zone_s *zone = zone_find(GET_PMMVAR(data));
227     if (!zone)
228         return -1;
229     struct pmmalloc_s **pinfo = &PMMAllocs;
230     for (;;) {
231         struct pmmalloc_s *info = GET_PMMVAR(*pinfo);
232         if (!info)
233             return -1;
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);
241             return 0;
242         }
243         pinfo = &info->next;
244     }
245     return -1;
246 }
247
248 // Find the amount of free space in a given zone.
249 static u32
250 pmm_getspace(struct zone_s *zone)
251 {
252     u32 space = GET_PMMVAR(zone->cur) - GET_PMMVAR(zone->bottom);
253     if (space <= PMMALLOCSIZE)
254         return 0;
255     return space - PMMALLOCSIZE;
256 }
257
258 // Find the data block allocated with pmm_malloc with a given handle.
259 static void *
260 pmm_find(u32 handle)
261 {
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);
266     return NULL;
267 }
268
269
270 /****************************************************************
271  * pmm interface
272  ****************************************************************/
273
274 struct pmmheader {
275     u32 signature;
276     u8 version;
277     u8 length;
278     u8 checksum;
279     u16 entry_offset;
280     u16 entry_seg;
281     u8 reserved[5];
282 } PACKED;
283
284 extern struct pmmheader PMMHEADER;
285
286 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
287
288 #if CONFIG_PMM
289 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
290     .version = 0x01,
291     .length = sizeof(PMMHEADER),
292     .entry_seg = SEG_BIOS,
293 };
294 #endif
295
296 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
297
298 // PMM - allocate
299 static u32
300 handle_pmm00(u16 *args)
301 {
302     u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
303     u16 flags = args[5];
304     dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
305             , length, handle, flags);
306     if (!length) {
307         // Memory size request
308         switch (flags & 3) {
309         default:
310         case 0:
311             return 0;
312         case 1:
313             return pmm_getspace(&ZoneTmpLow);
314         case 2:
315             return pmm_getspace(&ZoneTmpHigh);
316         case 3: {
317             u32 spacelow = pmm_getspace(&ZoneTmpLow);
318             u32 spacehigh = pmm_getspace(&ZoneTmpHigh);
319             if (spacelow > spacehigh)
320                 return spacelow;
321             return spacehigh;
322         }
323         }
324     }
325     u32 size = length * 16;
326     if ((s32)size <= 0)
327         return 0;
328     u32 align = MALLOC_MIN_ALIGN;
329     if (flags & 4) {
330         align = 1<<__ffs(size);
331         if (align < MALLOC_MIN_ALIGN)
332             align = MALLOC_MIN_ALIGN;
333     }
334     switch (flags & 3) {
335     default:
336     case 0:
337         return 0;
338     case 1:
339         return (u32)pmm_malloc(&ZoneTmpLow, handle, size, align);
340     case 2:
341         return (u32)pmm_malloc(&ZoneTmpHigh, handle, size, align);
342     case 3: {
343         void *data = pmm_malloc(&ZoneTmpLow, handle, size, align);
344         if (data)
345             return (u32)data;
346         return (u32)pmm_malloc(&ZoneTmpHigh, handle, size, align);
347     }
348     }
349 }
350
351 // PMM - find
352 static u32
353 handle_pmm01(u16 *args)
354 {
355     u32 handle = *(u32*)&args[1];
356     dprintf(3, "pmm01: handle=%x\n", handle);
357     if (handle == 0xFFFFFFFF)
358         return 0;
359     return (u32)pmm_find(handle);
360 }
361
362 // PMM - deallocate
363 static u32
364 handle_pmm02(u16 *args)
365 {
366     u32 buffer = *(u32*)&args[1];
367     dprintf(3, "pmm02: buffer=%x\n", buffer);
368     int ret = pmm_free((void*)buffer);
369     if (ret)
370         // Error
371         return 1;
372     return 0;
373 }
374
375 static u32
376 handle_pmmXX(u16 *args)
377 {
378     return PMM_FUNCTION_NOT_SUPPORTED;
379 }
380
381 u32 VISIBLE16
382 handle_pmm(u16 *args)
383 {
384     if (! CONFIG_PMM)
385         return PMM_FUNCTION_NOT_SUPPORTED;
386
387     u16 arg1 = args[0];
388     dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
389
390     switch (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);
395     }
396 }
397
398 // romlayout.S
399 extern void entry_pmm();
400
401 void
402 pmm_setup()
403 {
404     if (! CONFIG_PMM)
405         return;
406
407     dprintf(3, "init PMM\n");
408
409     PMMHEADER.signature = PMM_SIGNATURE;
410     PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
411     PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
412 }
413
414 void
415 pmm_finalize()
416 {
417     if (! CONFIG_PMM)
418         return;
419
420     dprintf(3, "finalize PMM\n");
421
422     PMMHEADER.signature = 0;
423     PMMHEADER.entry_offset = 0;
424 }