1 | /* |
---|
2 | * sbrk.c |
---|
3 | * |
---|
4 | * Authorship |
---|
5 | * ---------- |
---|
6 | * This software was created by |
---|
7 | * Till Straumann <strauman@slac.stanford.edu>, 2002, |
---|
8 | * Stanford Linear Accelerator Center, Stanford University. |
---|
9 | * |
---|
10 | * Acknowledgement of sponsorship |
---|
11 | * ------------------------------ |
---|
12 | * This software was produced by |
---|
13 | * the Stanford Linear Accelerator Center, Stanford University, |
---|
14 | * under Contract DE-AC03-76SFO0515 with the Department of Energy. |
---|
15 | * |
---|
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. |
---|
24 | * |
---|
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. |
---|
29 | * |
---|
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 |
---|
34 | * freely use it for any purpose without restriction. |
---|
35 | * |
---|
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. |
---|
44 | * |
---|
45 | * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03 |
---|
46 | */ |
---|
47 | |
---|
48 | /* |
---|
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 | |
---|
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; |
---|
73 | |
---|
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 | */ |
---|
82 | extern uintptr_t BSP_sbrk_policy __attribute__((weak)); |
---|
83 | |
---|
84 | #define LIMIT_32M 0x02000000 |
---|
85 | |
---|
86 | ptrdiff_t bsp_sbrk_init(Heap_Area *area, uintptr_t min_size) |
---|
87 | { |
---|
88 | uintptr_t rval = 0; |
---|
89 | uintptr_t policy; |
---|
90 | uintptr_t remaining_end; |
---|
91 | |
---|
92 | remaining_start = (uintptr_t) area->begin; |
---|
93 | remaining_size = area->size; |
---|
94 | remaining_end = remaining_start + remaining_size; |
---|
95 | |
---|
96 | if (remaining_start < LIMIT_32M && |
---|
97 | remaining_end > LIMIT_32M && |
---|
98 | min_size <= LIMIT_32M - remaining_start) { |
---|
99 | /* clip at LIMIT_32M */ |
---|
100 | rval = remaining_end - LIMIT_32M; |
---|
101 | area->size = LIMIT_32M - remaining_start; |
---|
102 | remaining_start = LIMIT_32M; |
---|
103 | remaining_size = rval; |
---|
104 | } |
---|
105 | |
---|
106 | policy = (0 == &BSP_sbrk_policy ? (uintptr_t)(-1) : BSP_sbrk_policy); |
---|
107 | switch ( policy ) { |
---|
108 | case (uintptr_t)(-1): |
---|
109 | area->size += rval; |
---|
110 | remaining_start = (uintptr_t) area->begin + area->size; |
---|
111 | remaining_size = 0; |
---|
112 | break; |
---|
113 | |
---|
114 | case 0: |
---|
115 | remaining_size = 0; |
---|
116 | break; |
---|
117 | |
---|
118 | default: |
---|
119 | if ( rval > policy ) |
---|
120 | rval = policy; |
---|
121 | break; |
---|
122 | } |
---|
123 | |
---|
124 | return (ptrdiff_t) (rval <= PTRDIFF_MAX ? rval : rval / 2); |
---|
125 | } |
---|
126 | |
---|
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") )); |
---|
132 | static void *bsp_sbrk(ptrdiff_t incr) |
---|
133 | { |
---|
134 | void *rval=(void*)-1; |
---|
135 | |
---|
136 | if ( remaining_start != INVALID_REMAINING_START && incr <= remaining_size) { |
---|
137 | remaining_size-=incr; |
---|
138 | rval = (void *) remaining_start; |
---|
139 | remaining_start += incr; |
---|
140 | } else { |
---|
141 | errno = ENOMEM; |
---|
142 | } |
---|
143 | #ifdef DEBUG |
---|
144 | printk("************* SBRK 0x%08x (ret 0x%08x) **********\n", incr, rval); |
---|
145 | #endif |
---|
146 | return rval; |
---|
147 | } |
---|