1 | /* $Id$ */ |
---|
2 | |
---|
3 | /* |
---|
4 | * sbrk.c |
---|
5 | * |
---|
6 | * Authorship |
---|
7 | * ---------- |
---|
8 | * This software was created by |
---|
9 | * Till Straumann <strauman@slac.stanford.edu>, 2002, |
---|
10 | * Stanford Linear Accelerator Center, Stanford University. |
---|
11 | * |
---|
12 | * Acknowledgement of sponsorship |
---|
13 | * ------------------------------ |
---|
14 | * This software was produced by |
---|
15 | * the Stanford Linear Accelerator Center, Stanford University, |
---|
16 | * under Contract DE-AC03-76SFO0515 with the Department of Energy. |
---|
17 | * |
---|
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. |
---|
26 | * |
---|
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. |
---|
31 | * |
---|
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 |
---|
36 | * freely use it for any purpose without restriction. |
---|
37 | * |
---|
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. |
---|
46 | * |
---|
47 | * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03 |
---|
48 | */ |
---|
49 | |
---|
50 | /* |
---|
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 | |
---|
73 | static uint32_t remaining_start=0; |
---|
74 | static uint32_t remaining_size=0; |
---|
75 | |
---|
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 | |
---|
86 | #define LIMIT_32M 0x02000000 |
---|
87 | |
---|
88 | uintptr_t _bsp_sbrk_init( |
---|
89 | uintptr_t heap_start, |
---|
90 | uintptr_t *heap_size_p |
---|
91 | ) |
---|
92 | { |
---|
93 | uintptr_t rval=0; |
---|
94 | |
---|
95 | remaining_start = heap_start; |
---|
96 | remaining_size = *heap_size_p; |
---|
97 | |
---|
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; |
---|
103 | remaining_start = LIMIT_32M; |
---|
104 | remaining_size = rval; |
---|
105 | } |
---|
106 | |
---|
107 | if ( 0 != &BSP_sbrk_policy ) { |
---|
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 | } |
---|
131 | } |
---|
132 | |
---|
133 | return rval; |
---|
134 | } |
---|
135 | |
---|
136 | void * sbrk(ptrdiff_t incr) |
---|
137 | { |
---|
138 | void *rval=(void*)-1; |
---|
139 | |
---|
140 | /* FIXME: BEWARE if size >2G */ |
---|
141 | if (incr <= remaining_size) { |
---|
142 | remaining_size-=incr; |
---|
143 | rval = (void*)remaining_start; |
---|
144 | remaining_start += incr; |
---|
145 | } else { |
---|
146 | errno = ENOMEM; |
---|
147 | } |
---|
148 | #ifdef DEBUG |
---|
149 | printk("************* SBRK 0x%08x (ret 0x%08x) **********\n", incr, rval); |
---|
150 | #endif |
---|
151 | return rval; |
---|
152 | } |
---|