[92b67b18] | 1 | /* |
---|
| 2 | * sbrk.c |
---|
| 3 | * |
---|
[f774fc06] | 4 | * Authorship |
---|
| 5 | * ---------- |
---|
| 6 | * This software was created by |
---|
| 7 | * Till Straumann <strauman@slac.stanford.edu>, 2002, |
---|
[344402c] | 8 | * Stanford Linear Accelerator Center, Stanford University. |
---|
[ac7af4a] | 9 | * |
---|
[f774fc06] | 10 | * Acknowledgement of sponsorship |
---|
| 11 | * ------------------------------ |
---|
| 12 | * This software was produced by |
---|
| 13 | * the Stanford Linear Accelerator Center, Stanford University, |
---|
[344402c] | 14 | * under Contract DE-AC03-76SFO0515 with the Department of Energy. |
---|
[ac7af4a] | 15 | * |
---|
[f774fc06] | 16 | * Government disclaimer of liability |
---|
| 17 | * ---------------------------------- |
---|
| 18 | * Neither the United States nor the United States Department of Energy, |
---|
| 19 | * nor any of their employees, makes any warranty, express or implied, or |
---|
| 20 | * assumes any legal liability or responsibility for the accuracy, |
---|
| 21 | * completeness, or usefulness of any data, apparatus, product, or process |
---|
| 22 | * disclosed, or represents that its use would not infringe privately owned |
---|
| 23 | * rights. |
---|
[ac7af4a] | 24 | * |
---|
[f774fc06] | 25 | * Stanford disclaimer of liability |
---|
| 26 | * -------------------------------- |
---|
| 27 | * Stanford University makes no representations or warranties, express or |
---|
| 28 | * implied, nor assumes any liability for the use of this software. |
---|
[ac7af4a] | 29 | * |
---|
[f774fc06] | 30 | * Stanford disclaimer of copyright |
---|
| 31 | * -------------------------------- |
---|
| 32 | * Stanford University, owner of the copyright, hereby disclaims its |
---|
| 33 | * copyright and all other rights in this software. Hence, anyone may |
---|
[ac7af4a] | 34 | * freely use it for any purpose without restriction. |
---|
| 35 | * |
---|
[f774fc06] | 36 | * Maintenance of notices |
---|
| 37 | * ---------------------- |
---|
| 38 | * In the interest of clarity regarding the origin and status of this |
---|
| 39 | * SLAC software, this and all the preceding Stanford University notices |
---|
| 40 | * are to remain affixed to any copy or derivative of this software made |
---|
| 41 | * or distributed by the recipient and are to be affixed to any copy of |
---|
| 42 | * software made or distributed by the recipient that contains a copy or |
---|
| 43 | * derivative of this software. |
---|
[ac7af4a] | 44 | * |
---|
[f774fc06] | 45 | * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03 |
---|
[ac7af4a] | 46 | */ |
---|
[f774fc06] | 47 | |
---|
| 48 | /* |
---|
[92b67b18] | 49 | * Hack around the 32bit powerpc 32M problem: |
---|
| 50 | * |
---|
| 51 | * GCC by default uses relative branches which can not jump |
---|
| 52 | * farther than 32M. Hence all program text is confined to |
---|
| 53 | * a single 32M segment. |
---|
| 54 | * This hack gives the RTEMS malloc region all memory below |
---|
| 55 | * 32M at startup. Only when this region is exhausted will sbrk |
---|
| 56 | * add more memory. Loading modules may fail at that point, hence |
---|
| 57 | * the user is expected to load all modules at startup _prior_ |
---|
| 58 | * to malloc()ing lots of memory... |
---|
| 59 | * |
---|
| 60 | * NOTE: it would probably be better to have a separate region |
---|
| 61 | * for module code. |
---|
| 62 | */ |
---|
| 63 | |
---|
| 64 | #include <errno.h> |
---|
| 65 | #include <unistd.h> |
---|
| 66 | |
---|
[47a3cd8] | 67 | #include <bsp/bootcard.h> |
---|
| 68 | |
---|
| 69 | #define INVALID_REMAINING_START ((uintptr_t) -1) |
---|
| 70 | |
---|
| 71 | static uintptr_t remaining_start = INVALID_REMAINING_START; |
---|
| 72 | static uintptr_t remaining_size = 0; |
---|
[92b67b18] | 73 | |
---|
[0612ad26] | 74 | /* App. may provide a value by defining the BSP_sbrk_policy |
---|
| 75 | * variable. |
---|
| 76 | * |
---|
| 77 | * (-1) -> give all memory to the heap at initialization time |
---|
| 78 | * > 0 -> value used as sbrk amount; initially give 32M |
---|
| 79 | * 0 -> limit memory effectively to 32M. |
---|
| 80 | * |
---|
| 81 | */ |
---|
[3da02169] | 82 | extern uintptr_t BSP_sbrk_policy[] __attribute__((weak)); |
---|
[0612ad26] | 83 | |
---|
[47a3cd8] | 84 | #define LIMIT_32M 0x02000000 |
---|
[92b67b18] | 85 | |
---|
[47a3cd8] | 86 | ptrdiff_t bsp_sbrk_init(Heap_Area *area, uintptr_t min_size) |
---|
[92b67b18] | 87 | { |
---|
[47a3cd8] | 88 | uintptr_t rval = 0; |
---|
[4f599ed] | 89 | uintptr_t policy; |
---|
[47a3cd8] | 90 | uintptr_t remaining_end; |
---|
[92b67b18] | 91 | |
---|
[47a3cd8] | 92 | remaining_start = (uintptr_t) area->begin; |
---|
| 93 | remaining_size = area->size; |
---|
| 94 | remaining_end = remaining_start + remaining_size; |
---|
[0612ad26] | 95 | |
---|
[92b67b18] | 96 | if (remaining_start < LIMIT_32M && |
---|
[47a3cd8] | 97 | remaining_end > LIMIT_32M && |
---|
| 98 | min_size <= LIMIT_32M - remaining_start) { |
---|
[92b67b18] | 99 | /* clip at LIMIT_32M */ |
---|
[47a3cd8] | 100 | rval = remaining_end - LIMIT_32M; |
---|
| 101 | area->size = LIMIT_32M - remaining_start; |
---|
[344402c] | 102 | remaining_start = LIMIT_32M; |
---|
| 103 | remaining_size = rval; |
---|
[92b67b18] | 104 | } |
---|
[0612ad26] | 105 | |
---|
[3da02169] | 106 | policy = (0 == BSP_sbrk_policy[0] ? (uintptr_t)(-1) : BSP_sbrk_policy[0]); |
---|
[4f599ed] | 107 | switch ( policy ) { |
---|
| 108 | case (uintptr_t)(-1): |
---|
[47a3cd8] | 109 | area->size += rval; |
---|
| 110 | remaining_start = (uintptr_t) area->begin + area->size; |
---|
[4f599ed] | 111 | remaining_size = 0; |
---|
| 112 | break; |
---|
[344402c] | 113 | |
---|
| 114 | case 0: |
---|
[4f599ed] | 115 | remaining_size = 0; |
---|
| 116 | break; |
---|
[344402c] | 117 | |
---|
| 118 | default: |
---|
[4f599ed] | 119 | if ( rval > policy ) |
---|
| 120 | rval = policy; |
---|
| 121 | break; |
---|
[0612ad26] | 122 | } |
---|
| 123 | |
---|
[47a3cd8] | 124 | return (ptrdiff_t) (rval <= PTRDIFF_MAX ? rval : rval / 2); |
---|
[92b67b18] | 125 | } |
---|
| 126 | |
---|
[344402c] | 127 | /* |
---|
| 128 | * This is just so the sbrk test can force its magic. All normal applications |
---|
| 129 | * should just use the default implementation in this file. |
---|
| 130 | */ |
---|
| 131 | void *sbrk(ptrdiff_t incr) __attribute__ (( weak, alias("bsp_sbrk") )); |
---|
[47a3cd8] | 132 | static void *bsp_sbrk(ptrdiff_t incr) |
---|
[92b67b18] | 133 | { |
---|
| 134 | void *rval=(void*)-1; |
---|
| 135 | |
---|
[47a3cd8] | 136 | if ( remaining_start != INVALID_REMAINING_START && incr <= remaining_size) { |
---|
[92b67b18] | 137 | remaining_size-=incr; |
---|
[47a3cd8] | 138 | rval = (void *) remaining_start; |
---|
[92b67b18] | 139 | remaining_start += incr; |
---|
| 140 | } else { |
---|
| 141 | errno = ENOMEM; |
---|
| 142 | } |
---|
[344402c] | 143 | #ifdef DEBUG |
---|
| 144 | printk("************* SBRK 0x%08x (ret 0x%08x) **********\n", incr, rval); |
---|
| 145 | #endif |
---|
[92b67b18] | 146 | return rval; |
---|
| 147 | } |
---|