2e9d1ff1c56b503827441540ba51da57963941a5
[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" // struct e820entry
10 #include "farptr.h" // GET_FARVAR
11 #include "biosvar.h" // GET_BDA
12
13
14 #if MODESEGMENT
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) ({                      \
18             SET_SEG(ES, 0);                     \
19             __GET_VAR("addr32 ", ES, (var)); })
20 #define SET_PMMVAR(var, val) do {               \
21         SET_SEG(ES, 0);                         \
22         __SET_VAR("addr32 ", ES, (var), (val)); \
23     } while (0)
24 #else
25 #define GET_PMMVAR(var) (var)
26 #define SET_PMMVAR(var, val) do { (var) = (val); } while (0)
27 #endif
28
29 // Information on a reserved area.
30 struct allocinfo_s {
31     struct allocinfo_s *next, **pprev;
32     void *data, *dataend, *allocend;
33 };
34
35 // Information on a tracked memory allocation.
36 struct allocdetail_s {
37     struct allocinfo_s detailinfo;
38     struct allocinfo_s datainfo;
39     u32 handle;
40 };
41
42 // The various memory zones.
43 struct zone_s {
44     struct allocinfo_s *info;
45 };
46
47 struct zone_s ZoneLow;
48 struct zone_s ZoneHigh;
49 struct zone_s ZoneFSeg;
50 struct zone_s ZoneTmpLow;
51 struct zone_s ZoneTmpHigh;
52
53 struct zone_s *Zones[] = {
54     &ZoneTmpLow, &ZoneLow, &ZoneFSeg, &ZoneTmpHigh, &ZoneHigh
55 };
56
57
58 /****************************************************************
59  * low-level memory reservations
60  ****************************************************************/
61
62 // Find and reserve space from a given zone
63 static void *
64 allocSpace(struct zone_s *zone, u32 size, u32 align, struct allocinfo_s *fill)
65 {
66     struct allocinfo_s *info;
67     for (info = GET_PMMVAR(zone->info); info; info = GET_PMMVAR(info->next)) {
68         void *dataend = GET_PMMVAR(info->dataend);
69         void *allocend = GET_PMMVAR(info->allocend);
70         void *newallocend = (void*)ALIGN_DOWN((u32)allocend - size, align);
71         if (newallocend >= dataend && newallocend <= allocend) {
72             // Found space - now reserve it.
73             struct allocinfo_s **pprev = GET_PMMVAR(info->pprev);
74             if (!fill)
75                 fill = newallocend;
76             SET_PMMVAR(fill->next, info);
77             SET_PMMVAR(fill->pprev, pprev);
78             SET_PMMVAR(fill->data, newallocend);
79             SET_PMMVAR(fill->dataend, newallocend + size);
80             SET_PMMVAR(fill->allocend, allocend);
81
82             SET_PMMVAR(info->allocend, newallocend);
83             SET_PMMVAR(info->pprev, &fill->next);
84             SET_PMMVAR(*pprev, fill);
85             return newallocend;
86         }
87     }
88     return NULL;
89 }
90
91 // Release space allocated with allocSpace()
92 static void
93 freeSpace(struct allocinfo_s *info)
94 {
95     struct allocinfo_s *next = GET_PMMVAR(info->next);
96     struct allocinfo_s **pprev = GET_PMMVAR(info->pprev);
97     SET_PMMVAR(*pprev, next);
98     if (next) {
99         if (GET_PMMVAR(next->allocend) == GET_PMMVAR(info->data))
100             SET_PMMVAR(next->allocend, GET_PMMVAR(info->allocend));
101         SET_PMMVAR(next->pprev, pprev);
102     }
103 }
104
105 // Add new memory to a zone
106 static void
107 addSpace(struct zone_s *zone, void *start, void *end)
108 {
109     // Find position to add space
110     struct allocinfo_s **pprev = &zone->info, *info;
111     for (;;) {
112         info = GET_PMMVAR(*pprev);
113         if (!info || GET_PMMVAR(info->data) < start)
114             break;
115         pprev = &info->next;
116     }
117
118     // Add space using temporary allocation info.
119     struct allocdetail_s tempdetail;
120     tempdetail.datainfo.next = info;
121     tempdetail.datainfo.pprev = pprev;
122     tempdetail.datainfo.data = tempdetail.datainfo.dataend = start;
123     tempdetail.datainfo.allocend = end;
124     struct allocdetail_s *tempdetailp = MAKE_FLATPTR(GET_SEG(SS), &tempdetail);
125     SET_PMMVAR(*pprev, &tempdetailp->datainfo);
126     if (info)
127         SET_PMMVAR(info->pprev, &tempdetailp->datainfo.next);
128
129     // Allocate final allocation info.
130     struct allocdetail_s *detail = allocSpace(
131         &ZoneTmpHigh, sizeof(*detail), MALLOC_MIN_ALIGN, NULL);
132     if (!detail) {
133         detail = allocSpace(&ZoneTmpLow, sizeof(*detail)
134                             , MALLOC_MIN_ALIGN, NULL);
135         if (!detail) {
136             SET_PMMVAR(*tempdetail.datainfo.pprev, tempdetail.datainfo.next);
137             if (tempdetail.datainfo.next)
138                 SET_PMMVAR(tempdetail.datainfo.next->pprev
139                            , tempdetail.datainfo.pprev);
140             warn_noalloc();
141             return;
142         }
143     }
144
145     // Replace temp alloc space with final alloc space
146     memcpy_fl(&detail->datainfo, &tempdetailp->datainfo
147               , sizeof(detail->datainfo));
148     SET_PMMVAR(detail->handle, PMM_DEFAULT_HANDLE);
149
150     SET_PMMVAR(*tempdetail.datainfo.pprev, &detail->datainfo);
151     if (tempdetail.datainfo.next)
152         SET_PMMVAR(tempdetail.datainfo.next->pprev, &detail->datainfo.next);
153 }
154
155 // Search all zones for an allocation obtained from allocSpace()
156 static struct allocinfo_s *
157 findAlloc(void *data)
158 {
159     int i;
160     for (i=0; i<ARRAY_SIZE(Zones); i++) {
161         struct zone_s *zone = GET_PMMVAR(Zones[i]);
162         struct allocinfo_s *info;
163         for (info = GET_PMMVAR(zone->info); info; info = GET_PMMVAR(info->next))
164             if (GET_PMMVAR(info->data) == data)
165                 return info;
166     }
167     return NULL;
168 }
169
170 // Return the last sentinal node of a zone
171 static struct allocinfo_s *
172 findLast(struct zone_s *zone)
173 {
174     struct allocinfo_s *info = GET_PMMVAR(zone->info);
175     if (!info)
176         return NULL;
177     for (;;) {
178         struct allocinfo_s *next = GET_PMMVAR(info->next);
179         if (!next)
180             return info;
181         info = next;
182     }
183 }
184
185
186 /****************************************************************
187  * Setup
188  ****************************************************************/
189
190 void
191 malloc_setup(void)
192 {
193     ASSERT32FLAT();
194     dprintf(3, "malloc setup\n");
195
196     // Populate temp high ram
197     u32 highram = 0;
198     int i;
199     for (i=e820_count-1; i>=0; i--) {
200         struct e820entry *en = &e820_list[i];
201         u64 end = en->start + en->size;
202         if (end < 1024*1024)
203             break;
204         if (en->type != E820_RAM || end > 0xffffffff)
205             continue;
206         u32 s = en->start, e = end;
207         if (!highram) {
208             u32 newe = ALIGN_DOWN(e - CONFIG_MAX_HIGHTABLE, MALLOC_MIN_ALIGN);
209             if (newe <= e && newe >= s) {
210                 highram = newe;
211                 e = newe;
212             }
213         }
214         addSpace(&ZoneTmpHigh, (void*)s, (void*)e);
215     }
216
217     // Populate other regions
218     addSpace(&ZoneTmpLow, (void*)BUILD_STACK_ADDR, (void*)BUILD_EBDA_MINIMUM);
219     addSpace(&ZoneFSeg, BiosTableSpace, &BiosTableSpace[CONFIG_MAX_BIOSTABLE]);
220     addSpace(&ZoneLow, (void*)BUILD_LOWRAM_END, (void*)BUILD_LOWRAM_END);
221     if (highram) {
222         addSpace(&ZoneHigh, (void*)highram
223                  , (void*)highram + CONFIG_MAX_HIGHTABLE);
224         add_e820(highram, CONFIG_MAX_HIGHTABLE, E820_RESERVED);
225     }
226 }
227
228 // Update pointers after code relocation.
229 void
230 malloc_fixupreloc(void)
231 {
232     ASSERT32FLAT();
233     if (!CONFIG_RELOCATE_INIT)
234         return;
235     dprintf(3, "malloc fixup reloc\n");
236
237     int i;
238     for (i=0; i<ARRAY_SIZE(Zones); i++) {
239         struct zone_s *zone = Zones[i];
240         zone->info->pprev = &zone->info;
241     }
242 }
243
244 void
245 malloc_finalize(void)
246 {
247     dprintf(3, "malloc finalize\n");
248
249     // Reserve more low-mem if needed.
250     u32 endlow = GET_BDA(mem_size_kb)*1024;
251     add_e820(endlow, BUILD_LOWRAM_END-endlow, E820_RESERVED);
252
253     // Give back unused high ram.
254     struct allocinfo_s *info = findLast(&ZoneHigh);
255     if (info) {
256         u32 giveback = ALIGN_DOWN(info->allocend - info->dataend, PAGE_SIZE);
257         add_e820((u32)info->dataend, giveback, E820_RAM);
258         dprintf(1, "Returned %d bytes of ZoneHigh\n", giveback);
259     }
260 }
261
262
263 /****************************************************************
264  * ebda movement
265  ****************************************************************/
266
267 // Move ebda
268 static int
269 relocate_ebda(u32 newebda, u32 oldebda, u8 ebda_size)
270 {
271     u32 lowram = GET_BDA(mem_size_kb) * 1024;
272     if (oldebda != lowram)
273         // EBDA isn't at end of ram - give up.
274         return -1;
275
276     // Do copy (this assumes memcpy copies forward - otherwise memmove
277     // is needed)
278     memcpy_fl((void*)newebda, (void*)oldebda, ebda_size * 1024);
279
280     // Update indexes
281     dprintf(1, "ebda moved from %x to %x\n", oldebda, newebda);
282     SET_BDA(mem_size_kb, newebda / 1024);
283     SET_BDA(ebda_seg, FLATPTR_TO_SEG(newebda));
284     return 0;
285 }
286
287 // Support expanding the ZoneLow dynamically.
288 static void
289 zonelow_expand(u32 size, u32 align)
290 {
291     struct allocinfo_s *info = findLast(&ZoneLow);
292     if (!info)
293         return;
294     u32 oldpos = (u32)GET_PMMVAR(info->allocend);
295     u32 newpos = ALIGN_DOWN(oldpos - size, align);
296     u32 bottom = (u32)GET_PMMVAR(info->dataend);
297     if (newpos >= bottom && newpos <= oldpos)
298         // Space already present.
299         return;
300     u16 ebda_seg = get_ebda_seg();
301     u32 ebda_pos = (u32)MAKE_FLATPTR(ebda_seg, 0);
302     u8 ebda_size = GET_EBDA2(ebda_seg, size);
303     u32 ebda_end = ebda_pos + ebda_size * 1024;
304     if (ebda_end != bottom)
305         // Something else is after ebda - can't use any existing space.
306         newpos = ALIGN_DOWN(ebda_end - size, align);
307     u32 newbottom = ALIGN_DOWN(newpos, 1024);
308     u32 newebda = ALIGN_DOWN(newbottom - ebda_size * 1024, 1024);
309     if (newebda < BUILD_EBDA_MINIMUM)
310         // Not enough space.
311         return;
312
313     // Move ebda
314     int ret = relocate_ebda(newebda, ebda_pos, ebda_size);
315     if (ret)
316         return;
317
318     // Update zone
319     if (ebda_end == bottom) {
320         SET_PMMVAR(info->data, (void*)newbottom);
321         SET_PMMVAR(info->dataend, (void*)newbottom);
322     } else
323         addSpace(&ZoneLow, (void*)newbottom, (void*)ebda_end);
324 }
325
326 // Check if can expand the given zone to fulfill an allocation
327 static void *
328 allocExpandSpace(struct zone_s *zone, u32 size, u32 align
329                  , struct allocinfo_s *fill)
330 {
331     void *data = allocSpace(zone, size, align, fill);
332     if (data || zone != &ZoneLow)
333         return data;
334
335     // Make sure to not move ebda while an optionrom is running.
336     if (unlikely(wait_preempt())) {
337         data = allocSpace(zone, size, align, fill);
338         if (data)
339             return data;
340     }
341
342     zonelow_expand(size, align);
343     return allocSpace(zone, size, align, fill);
344 }
345
346
347 /****************************************************************
348  * tracked memory allocations
349  ****************************************************************/
350
351 // Allocate memory from the given zone and track it as a PMM allocation
352 void * __malloc
353 pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align)
354 {
355     if (!size)
356         return NULL;
357
358     // Find and reserve space for bookkeeping.
359     struct allocdetail_s *detail = allocSpace(
360         &ZoneTmpHigh, sizeof(*detail), MALLOC_MIN_ALIGN, NULL);
361     if (!detail) {
362         detail = allocSpace(&ZoneTmpLow, sizeof(*detail)
363                             , MALLOC_MIN_ALIGN, NULL);
364         if (!detail)
365             return NULL;
366     }
367
368     // Find and reserve space for main allocation
369     void *data = allocExpandSpace(zone, size, align, &detail->datainfo);
370     if (!data) {
371         freeSpace(&detail->detailinfo);
372         return NULL;
373     }
374
375     dprintf(8, "pmm_malloc zone=%p handle=%x size=%d align=%x"
376             " ret=%p (detail=%p)\n"
377             , zone, handle, size, align
378             , data, detail);
379     SET_PMMVAR(detail->handle, handle);
380
381     return data;
382 }
383
384 // Free a data block allocated with pmm_malloc
385 int
386 pmm_free(void *data)
387 {
388     struct allocinfo_s *info = findAlloc(data);
389     if (!info || data == (void*)info || data == GET_PMMVAR(info->dataend))
390         return -1;
391     struct allocdetail_s *detail = container_of(
392         info, struct allocdetail_s, datainfo);
393     dprintf(8, "pmm_free %p (detail=%p)\n", data, detail);
394     freeSpace(info);
395     freeSpace(&detail->detailinfo);
396     return 0;
397 }
398
399 // Find the amount of free space in a given zone.
400 static u32
401 pmm_getspace(struct zone_s *zone)
402 {
403     // XXX - doesn't account for ZoneLow being able to grow.
404     // XXX - results not reliable when CONFIG_THREAD_OPTIONROMS
405     u32 maxspace = 0;
406     struct allocinfo_s *info;
407     for (info = GET_PMMVAR(zone->info); info; info = GET_PMMVAR(info->next)) {
408         u32 space = GET_PMMVAR(info->allocend) - GET_PMMVAR(info->dataend);
409         if (space > maxspace)
410             maxspace = space;
411     }
412
413     if (zone != &ZoneTmpHigh && zone != &ZoneTmpLow)
414         return maxspace;
415     // Account for space needed for PMM tracking.
416     u32 reserve = ALIGN(sizeof(struct allocdetail_s), MALLOC_MIN_ALIGN);
417     if (maxspace <= reserve)
418         return 0;
419     return maxspace - reserve;
420 }
421
422 // Find the data block allocated with pmm_malloc with a given handle.
423 static void *
424 pmm_find(u32 handle)
425 {
426     int i;
427     for (i=0; i<ARRAY_SIZE(Zones); i++) {
428         struct zone_s *zone = GET_PMMVAR(Zones[i]);
429         struct allocinfo_s *info;
430         for (info = GET_PMMVAR(zone->info); info
431                  ; info = GET_PMMVAR(info->next)) {
432             if (GET_PMMVAR(info->data) != (void*)info)
433                 continue;
434             struct allocdetail_s *detail = container_of(
435                 info, struct allocdetail_s, detailinfo);
436             if (GET_PMMVAR(detail->handle) == handle)
437                 return GET_PMMVAR(detail->datainfo.data);
438         }
439     }
440     return NULL;
441 }
442
443
444 /****************************************************************
445  * pmm interface
446  ****************************************************************/
447
448 struct pmmheader {
449     u32 signature;
450     u8 version;
451     u8 length;
452     u8 checksum;
453     u16 entry_offset;
454     u16 entry_seg;
455     u8 reserved[5];
456 } PACKED;
457
458 extern struct pmmheader PMMHEADER;
459
460 #define PMM_SIGNATURE 0x4d4d5024 // $PMM
461
462 #if CONFIG_PMM
463 struct pmmheader PMMHEADER __aligned(16) VAR16EXPORT = {
464     .version = 0x01,
465     .length = sizeof(PMMHEADER),
466     .entry_seg = SEG_BIOS,
467 };
468 #endif
469
470 #define PMM_FUNCTION_NOT_SUPPORTED 0xffffffff
471
472 // PMM - allocate
473 static u32
474 handle_pmm00(u16 *args)
475 {
476     u32 length = *(u32*)&args[1], handle = *(u32*)&args[3];
477     u16 flags = args[5];
478     dprintf(3, "pmm00: length=%x handle=%x flags=%x\n"
479             , length, handle, flags);
480     struct zone_s *lowzone = &ZoneTmpLow, *highzone = &ZoneTmpHigh;
481     if (flags & 8) {
482         // Permanent memory request.
483         lowzone = &ZoneLow;
484         highzone = &ZoneHigh;
485     }
486     if (!length) {
487         // Memory size request
488         switch (flags & 3) {
489         default:
490         case 0:
491             return 0;
492         case 1:
493             return pmm_getspace(lowzone);
494         case 2:
495             return pmm_getspace(highzone);
496         case 3: {
497             u32 spacelow = pmm_getspace(lowzone);
498             u32 spacehigh = pmm_getspace(highzone);
499             if (spacelow > spacehigh)
500                 return spacelow;
501             return spacehigh;
502         }
503         }
504     }
505     u32 size = length * 16;
506     if ((s32)size <= 0)
507         return 0;
508     u32 align = MALLOC_MIN_ALIGN;
509     if (flags & 4) {
510         align = 1<<__ffs(size);
511         if (align < MALLOC_MIN_ALIGN)
512             align = MALLOC_MIN_ALIGN;
513     }
514     switch (flags & 3) {
515     default:
516     case 0:
517         return 0;
518     case 1:
519         return (u32)pmm_malloc(lowzone, handle, size, align);
520     case 2:
521         return (u32)pmm_malloc(highzone, handle, size, align);
522     case 3: {
523         void *data = pmm_malloc(lowzone, handle, size, align);
524         if (data)
525             return (u32)data;
526         return (u32)pmm_malloc(highzone, handle, size, align);
527     }
528     }
529 }
530
531 // PMM - find
532 static u32
533 handle_pmm01(u16 *args)
534 {
535     u32 handle = *(u32*)&args[1];
536     dprintf(3, "pmm01: handle=%x\n", handle);
537     if (handle == PMM_DEFAULT_HANDLE)
538         return 0;
539     return (u32)pmm_find(handle);
540 }
541
542 // PMM - deallocate
543 static u32
544 handle_pmm02(u16 *args)
545 {
546     u32 buffer = *(u32*)&args[1];
547     dprintf(3, "pmm02: buffer=%x\n", buffer);
548     int ret = pmm_free((void*)buffer);
549     if (ret)
550         // Error
551         return 1;
552     return 0;
553 }
554
555 static u32
556 handle_pmmXX(u16 *args)
557 {
558     return PMM_FUNCTION_NOT_SUPPORTED;
559 }
560
561 u32 VISIBLE32INIT
562 handle_pmm(u16 *args)
563 {
564     if (! CONFIG_PMM)
565         return PMM_FUNCTION_NOT_SUPPORTED;
566
567     u16 arg1 = args[0];
568     dprintf(DEBUG_HDL_pmm, "pmm call arg1=%x\n", arg1);
569
570     int oldpreempt;
571     if (CONFIG_THREAD_OPTIONROMS) {
572         // Not a preemption event - don't wait in wait_preempt()
573         oldpreempt = CanPreempt;
574         CanPreempt = 0;
575     }
576
577     u32 ret;
578     switch (arg1) {
579     case 0x00: ret = handle_pmm00(args); break;
580     case 0x01: ret = handle_pmm01(args); break;
581     case 0x02: ret = handle_pmm02(args); break;
582     default:   ret = handle_pmmXX(args); break;
583     }
584
585     if (CONFIG_THREAD_OPTIONROMS)
586         CanPreempt = oldpreempt;
587
588     return ret;
589 }
590
591 // romlayout.S
592 extern void entry_pmm(void);
593
594 void
595 pmm_setup(void)
596 {
597     if (! CONFIG_PMM)
598         return;
599
600     dprintf(3, "init PMM\n");
601
602     PMMHEADER.signature = PMM_SIGNATURE;
603     PMMHEADER.entry_offset = (u32)entry_pmm - BUILD_BIOS_ADDR;
604     PMMHEADER.checksum -= checksum(&PMMHEADER, sizeof(PMMHEADER));
605 }
606
607 void
608 pmm_finalize(void)
609 {
610     if (! CONFIG_PMM)
611         return;
612
613     dprintf(3, "finalize PMM\n");
614
615     PMMHEADER.signature = 0;
616     PMMHEADER.entry_offset = 0;
617 }