Add initial support for PMM allocations of permanent memory.
[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" // find_high_area
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 ZoneLow VAR32VISIBLE, ZoneHigh VAR32VISIBLE;
34 struct zone_s ZoneFSeg VAR32VISIBLE;
35 struct zone_s ZoneTmpLow VAR32VISIBLE, ZoneTmpHigh VAR32VISIBLE;
36
37 struct zone_s *Zones[] VAR32VISIBLE = {
38     &ZoneTmpLow, &ZoneLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
39 };
40
41 // Obtain memory from a given zone.
42 static void *
43 zone_malloc(struct zone_s *zone, u32 size, u32 align)
44 {
45     u32 newpos = (GET_PMMVAR(zone->cur) - size) / align * align;
46     if ((s32)(newpos - GET_PMMVAR(zone->bottom)) < 0)
47         // No space
48         return NULL;
49     SET_PMMVAR(zone->cur, newpos);
50     return (void*)newpos;
51 }
52
53 // Return memory to a zone (if it was the last to be allocated).
54 static void
55 zone_free(struct zone_s *zone, void *data, u32 olddata)
56 {
57     if (! data || GET_PMMVAR(zone->cur) != (u32)data)
58         return;
59     SET_PMMVAR(zone->cur, olddata);
60 }
61
62 // Find the zone that contains the given data block.
63 static struct zone_s *
64 zone_find(void *data)
65 {
66     int i;
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))
71             return zone;
72     }
73     return NULL;
74 }
75
76 // Report the status of all the zones.
77 static void
78 dumpZones()
79 {
80     int i;
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);
88     }
89 }
90
91 // Allocate memory at the top of 32bit ram.
92 void *
93 malloc_high(u32 size)
94 {
95     return zone_malloc(&ZoneHigh, size, MALLOC_MIN_ALIGN);
96 }
97
98 // Allocate memory in the 0xf0000-0x100000 area of ram.
99 void *
100 malloc_fseg(u32 size)
101 {
102     return zone_malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
103 }
104
105 void
106 malloc_setup()
107 {
108     ASSERT32();
109     dprintf(3, "malloc setup\n");
110
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;
115
116     // Memory under 1Meg.
117     ZoneTmpLow.bottom = BUILD_STACK_ADDR;
118     ZoneTmpLow.top = ZoneTmpLow.cur = (u32)MAKE_FLATPTR(EBDA_SEGMENT_MINIMUM, 0);
119
120     // Permanent memory under 1Meg.  XXX - not implemented yet.
121     ZoneLow.bottom = ZoneLow.top = ZoneLow.cur = 0xa0000;
122
123     // Find memory at the top of ram.
124     struct e820entry *e = find_high_area(CONFIG_MAX_HIGHTABLE+MALLOC_MIN_ALIGN);
125     if (!e) {
126         // No memory above 1Meg
127         memset(&ZoneHigh, 0, sizeof(ZoneHigh));
128         memset(&ZoneTmpHigh, 0, sizeof(ZoneTmpHigh));
129         return;
130     }
131     u32 top = e->start + e->size, bottom = e->start;
132
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);
137
138     // Memory above 1Meg
139     ZoneTmpHigh.bottom = ALIGN(bottom, MALLOC_MIN_ALIGN);
140     ZoneTmpHigh.top = ZoneTmpHigh.cur = ZoneHigh.bottom;
141 }
142
143 void
144 malloc_finalize()
145 {
146     dprintf(3, "malloc finalize\n");
147
148     dumpZones();
149
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);
154
155     // Clear low-memory allocations.
156     memset((void*)ZoneTmpLow.bottom, 0, ZoneTmpLow.top - ZoneTmpLow.bottom);
157 }
158
159
160 /****************************************************************
161  * pmm allocation
162  ****************************************************************/
163
164 // Information on PMM tracked allocations
165 struct pmmalloc_s {
166     void *data;
167     u32 olddata;
168     u32 handle;
169     u32 oldallocdata;
170     struct pmmalloc_s *next;
171 };
172
173 struct pmmalloc_s *PMMAllocs VAR32VISIBLE;
174
175 // Memory zone that pmm allocation tracking info is stored in
176 #define ZONEALLOC (&ZoneTmpHigh)
177
178 // Allocate memory from the given zone and track it as a PMM allocation
179 static void *
180 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
181 {
182     u32 oldallocdata = GET_PMMVAR(ZONEALLOC->cur);
183     struct pmmalloc_s *info = zone_malloc(ZONEALLOC, sizeof(*info)
184                                           , MALLOC_MIN_ALIGN);
185     if (!info)
186         return NULL;
187     u32 olddata = GET_PMMVAR(zone->cur);
188     void *data = zone_malloc(zone, size, align);
189     if (! data) {
190         zone_free(ZONEALLOC, info, oldallocdata);
191         return NULL;
192     }
193     dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
194             " ret=%p (info=%p)\n"
195             , zone, handle, size, align
196             , data, info);
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);
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         } else if (GET_PMMVAR(info->oldallocdata) == (u32)data) {
220             SET_PMMVAR(info->oldallocdata, olddata);
221             return;
222         }
223 }
224
225 // Free a data block allocated with pmm_malloc
226 static int
227 pmm_free(void *data)
228 {
229     struct zone_s *zone = zone_find(GET_PMMVAR(data));
230     if (!zone)
231         return -1;
232     struct pmmalloc_s **pinfo = &PMMAllocs;
233     for (;;) {
234         struct pmmalloc_s *info = GET_PMMVAR(*pinfo);
235         if (!info)
236             return -1;
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"
244                     " info=%p\n"
245                     , data, zone, (void*)olddata, (void*)oldallocdata
246                     , info);
247             return 0;
248         }
249         pinfo = &info->next;
250     }
251 }
252
253 // Find the amount of free space in a given zone.
254 static u32
255 pmm_getspace(struct zone_s *zone)
256 {
257     u32 space = GET_PMMVAR(zone->cur) - GET_PMMVAR(zone->bottom);
258     if (zone != ZONEALLOC)
259         return space;
260     u32 reserve = ALIGN(sizeof(struct pmmalloc_s), MALLOC_MIN_ALIGN);
261     if (space <= reserve)
262         return 0;
263     return space - reserve;
264 }
265
266 // Find the data block allocated with pmm_malloc with a given handle.
267 static void *
268 pmm_find(u32 handle)
269 {
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);
274     return NULL;
275 }
276
277
278 /****************************************************************
279  * pmm interface
280  ****************************************************************/
281
282 struct pmmheader {
283     u32 signature;
284     u8 version;
285     u8 length;
286     u8 checksum;
287     u16 entry_offset;
288     u16 entry_seg;
289     u8 reserved[5];
290 } PACKED;
291
292 extern struct pmmheader PMMHEADER;
293
294 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
295
296 #if CONFIG_PMM
297 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
298     .version = 0x01,
299     .length = sizeof(PMMHEADER),
300     .entry_seg = SEG_BIOS,
301 };
302 #endif
303
304 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
305
306 // PMM - allocate
307 static u32
308 handle_pmm00(u16 *args)
309 {
310     u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
311     u16 flags = args[5];
312     dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
313             , length, handle, flags);
314     struct zone_s *lowzone = &ZoneTmpLow, *highzone = &ZoneTmpHigh;
315     if (flags & 8) {
316         // Permanent memory request.
317         lowzone = &ZoneLow;
318         highzone = &ZoneHigh;
319     }
320     if (!length) {
321         // Memory size request
322         switch (flags & 3) {
323         default:
324         case 0:
325             return 0;
326         case 1:
327             return pmm_getspace(lowzone);
328         case 2:
329             return pmm_getspace(highzone);
330         case 3: {
331             u32 spacelow = pmm_getspace(lowzone);
332             u32 spacehigh = pmm_getspace(highzone);
333             if (spacelow > spacehigh)
334                 return spacelow;
335             return spacehigh;
336         }
337         }
338     }
339     u32 size = length * 16;
340     if ((s32)size <= 0)
341         return 0;
342     u32 align = MALLOC_MIN_ALIGN;
343     if (flags & 4) {
344         align = 1<<__ffs(size);
345         if (align < MALLOC_MIN_ALIGN)
346             align = MALLOC_MIN_ALIGN;
347     }
348     switch (flags & 3) {
349     default:
350     case 0:
351         return 0;
352     case 1:
353         return (u32)pmm_malloc(lowzone, handle, size, align);
354     case 2:
355         return (u32)pmm_malloc(highzone, handle, size, align);
356     case 3: {
357         void *data = pmm_malloc(lowzone, handle, size, align);
358         if (data)
359             return (u32)data;
360         return (u32)pmm_malloc(highzone, handle, size, align);
361     }
362     }
363 }
364
365 // PMM - find
366 static u32
367 handle_pmm01(u16 *args)
368 {
369     u32 handle = *(u32*)&args[1];
370     dprintf(3, "pmm01: handle=%x\n", handle);
371     if (handle == 0xFFFFFFFF)
372         return 0;
373     return (u32)pmm_find(handle);
374 }
375
376 // PMM - deallocate
377 static u32
378 handle_pmm02(u16 *args)
379 {
380     u32 buffer = *(u32*)&args[1];
381     dprintf(3, "pmm02: buffer=%x\n", buffer);
382     int ret = pmm_free((void*)buffer);
383     if (ret)
384         // Error
385         return 1;
386     return 0;
387 }
388
389 static u32
390 handle_pmmXX(u16 *args)
391 {
392     return PMM_FUNCTION_NOT_SUPPORTED;
393 }
394
395 u32 VISIBLE16
396 handle_pmm(u16 *args)
397 {
398     if (! CONFIG_PMM)
399         return PMM_FUNCTION_NOT_SUPPORTED;
400
401     u16 arg1 = args[0];
402     dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
403
404     switch (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);
409     }
410 }
411
412 // romlayout.S
413 extern void entry_pmm();
414
415 void
416 pmm_setup()
417 {
418     if (! CONFIG_PMM)
419         return;
420
421     dprintf(3, "init PMM\n");
422
423     PMMAllocs = NULL;
424
425     PMMHEADER.signature = PMM_SIGNATURE;
426     PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
427     PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
428 }
429
430 void
431 pmm_finalize()
432 {
433     if (! CONFIG_PMM)
434         return;
435
436     dprintf(3, "finalize PMM\n");
437
438     PMMHEADER.signature = 0;
439     PMMHEADER.entry_offset = 0;
440 }