Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <console/console.h> |
Duncan Laurie | f5e9ac4 | 2012-06-23 13:33:32 -0700 | [diff] [blame] | 3 | #include <cpu/x86/smm.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 4 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 5 | #if CONFIG(DEBUG_MALLOC) |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 6 | #define MALLOCDBG(x...) printk(BIOS_SPEW, x) |
Uwe Hermann | a953f37 | 2010-11-10 00:14:32 +0000 | [diff] [blame] | 7 | #else |
Peter Stuge | 5015f79 | 2010-11-10 02:00:32 +0000 | [diff] [blame] | 8 | #define MALLOCDBG(x...) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 9 | #endif |
Uwe Hermann | a953f37 | 2010-11-10 00:14:32 +0000 | [diff] [blame] | 10 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 11 | extern unsigned char _heap, _eheap; |
Stefan Reinauer | 054c723 | 2009-03-06 17:22:35 +0000 | [diff] [blame] | 12 | static void *free_mem_ptr = &_heap; /* Start of heap */ |
| 13 | static void *free_mem_end_ptr = &_eheap; /* End of heap */ |
Bora Guvendik | a347ea3 | 2019-12-23 16:44:06 -0800 | [diff] [blame] | 14 | static void *free_last_alloc_ptr = &_heap; /* End of heap before |
| 15 | last allocation */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 16 | |
Ron Minnich | eb59636 | 2012-04-11 10:30:15 -0700 | [diff] [blame] | 17 | /* We don't restrict the boundary. This is firmware, |
| 18 | * you are supposed to know what you are doing. |
| 19 | */ |
| 20 | void *memalign(size_t boundary, size_t size) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 21 | { |
| 22 | void *p; |
| 23 | |
Ronald G. Minnich | 79431f5 | 2012-05-31 16:02:26 -0700 | [diff] [blame] | 24 | MALLOCDBG("%s Enter, boundary %zu, size %zu, free_mem_ptr %p\n", |
Ron Minnich | eb59636 | 2012-04-11 10:30:15 -0700 | [diff] [blame] | 25 | __func__, boundary, size, free_mem_ptr); |
Stefan Reinauer | 6bd571e | 2009-09-25 21:59:57 +0000 | [diff] [blame] | 26 | |
Ron Minnich | eb59636 | 2012-04-11 10:30:15 -0700 | [diff] [blame] | 27 | free_mem_ptr = (void *)ALIGN((unsigned long)free_mem_ptr, boundary); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 28 | |
Ron Minnich | eb59636 | 2012-04-11 10:30:15 -0700 | [diff] [blame] | 29 | p = free_mem_ptr; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 30 | free_mem_ptr += size; |
Bora Guvendik | a347ea3 | 2019-12-23 16:44:06 -0800 | [diff] [blame] | 31 | /* |
| 32 | * Store last allocation pointer after ALIGN, as malloc() will |
| 33 | * return it. This may cause n bytes of gap between allocations |
| 34 | * where n < boundary. |
| 35 | */ |
| 36 | free_last_alloc_ptr = p; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 37 | |
Ronald G. Minnich | 79431f5 | 2012-05-31 16:02:26 -0700 | [diff] [blame] | 38 | if (free_mem_ptr >= free_mem_end_ptr) { |
| 39 | printk(BIOS_ERR, "memalign(boundary=%zu, size=%zu): failed: ", |
| 40 | boundary, size); |
| 41 | printk(BIOS_ERR, "Tried to round up free_mem_ptr %p to %p\n", |
| 42 | p, free_mem_ptr); |
| 43 | printk(BIOS_ERR, "but free_mem_end_ptr is %p\n", |
| 44 | free_mem_end_ptr); |
Ron Minnich | eb59636 | 2012-04-11 10:30:15 -0700 | [diff] [blame] | 45 | die("Error! memalign: Out of memory (free_mem_ptr >= free_mem_end_ptr)"); |
Ronald G. Minnich | 79431f5 | 2012-05-31 16:02:26 -0700 | [diff] [blame] | 46 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 47 | |
Ron Minnich | eb59636 | 2012-04-11 10:30:15 -0700 | [diff] [blame] | 48 | MALLOCDBG("memalign %p\n", p); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 49 | |
| 50 | return p; |
| 51 | } |
| 52 | |
Ron Minnich | eb59636 | 2012-04-11 10:30:15 -0700 | [diff] [blame] | 53 | void *malloc(size_t size) |
| 54 | { |
| 55 | return memalign(sizeof(u64), size); |
| 56 | } |
Bora Guvendik | a347ea3 | 2019-12-23 16:44:06 -0800 | [diff] [blame] | 57 | |
| 58 | void free(void *ptr) |
| 59 | { |
| 60 | if (ptr == NULL) |
| 61 | return; |
| 62 | |
| 63 | if (ptr < (void *)&_heap || ptr >= free_mem_end_ptr) { |
| 64 | printk(BIOS_WARNING, "Warning - Pointer passed to %s is not " |
| 65 | "pointing to the heap\n", __func__); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Rewind the heap pointer to the end of heap |
| 71 | * before the last successful malloc(). |
| 72 | */ |
| 73 | if (ptr == free_last_alloc_ptr) { |
| 74 | free_mem_ptr = free_last_alloc_ptr; |
| 75 | free_last_alloc_ptr = NULL; |
| 76 | } |
| 77 | } |