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