1 | /* impure.c. Handling of re-entrancy data structure for OpenRISC 1000. |
---|
2 | |
---|
3 | Copyright (C) 2014, Authors |
---|
4 | |
---|
5 | Contributor Stefan Wallentowitz <stefan.wallentowitz@tum.de> |
---|
6 | |
---|
7 | * The authors hereby grant permission to use, copy, modify, distribute, |
---|
8 | * and license this software and its documentation for any purpose, provided |
---|
9 | * that existing copyright notices are retained in all copies and that this |
---|
10 | * notice is included verbatim in any distributions. No written agreement, |
---|
11 | * license, or royalty fee is required for any of the authorized uses. |
---|
12 | * Modifications to this software may be copyrighted by their authors |
---|
13 | * and need not follow the licensing terms described here, provided that |
---|
14 | * the new terms are clearly indicated on the first page of each file where |
---|
15 | * they apply. |
---|
16 | */ |
---|
17 | |
---|
18 | #include <reent.h> |
---|
19 | #include "or1k-internals.h" |
---|
20 | |
---|
21 | #include <string.h> |
---|
22 | |
---|
23 | /* As an exception handler may also use the library, it is better to use |
---|
24 | * a different re-entrancy data structure for the exceptions. |
---|
25 | * This data structure is configured here and as part of the exception |
---|
26 | * handler (or1k_exception_handler) temporarily replaces the software's |
---|
27 | * impure data pointer. |
---|
28 | * |
---|
29 | * During initialization, the libraries standard _impure_data and the exception |
---|
30 | * impure data (_exception_impure_data) are initialized. Afterwards, |
---|
31 | * the current value _current_impure_ptr is set to _impure_ptr. |
---|
32 | * |
---|
33 | * At runtime __getreent is called to return the current reentrancy pointer, |
---|
34 | * which is stored in _current_impure_ptr. |
---|
35 | * |
---|
36 | * In the or1k_exception_handler the _current_impure_ptr is set to point to |
---|
37 | * _exception_impure_ptr. After the exception handler returned, it is set back |
---|
38 | * to _impure_ptr. |
---|
39 | */ |
---|
40 | |
---|
41 | /* Link in the external impure_data structure */ |
---|
42 | extern struct _reent *__ATTRIBUTE_IMPURE_PTR__ _impure_ptr; |
---|
43 | |
---|
44 | #ifdef __OR1K_MULTICORE__ |
---|
45 | struct _reent **_or1k_impure_ptr; |
---|
46 | struct _reent **_or1k_exception_impure_ptr; |
---|
47 | struct _reent **_or1k_current_impure_ptr; |
---|
48 | #else |
---|
49 | struct _reent *__ATTRIBUTE_IMPURE_PTR__ _or1k_impure_ptr; |
---|
50 | |
---|
51 | /* Create exception impure data structure */ |
---|
52 | static struct _reent _or1k_exception_impure_data = _REENT_INIT (_or1k_exception_impure_data); |
---|
53 | |
---|
54 | /* Link to the exception impure data structure */ |
---|
55 | struct _reent *__ATTRIBUTE_IMPURE_PTR__ _or1k_exception_impure_ptr = &_or1k_exception_impure_data; |
---|
56 | |
---|
57 | /* Link to the currently used data structure. */ |
---|
58 | struct _reent *__ATTRIBUTE_IMPURE_PTR__ _or1k_current_impure_ptr; |
---|
59 | #endif |
---|
60 | |
---|
61 | #ifdef __OR1K_MULTICORE__ |
---|
62 | #define OR1K_LIBC_GETREENT _or1k_current_impure_ptr[or1k_coreid()] |
---|
63 | #else |
---|
64 | #define OR1K_LIBC_GETREENT _or1k_current_impure_ptr |
---|
65 | #endif |
---|
66 | |
---|
67 | void |
---|
68 | _or1k_libc_impure_init (void) |
---|
69 | { |
---|
70 | #ifdef __OR1K_MULTICORE__ |
---|
71 | uint32_t c; |
---|
72 | |
---|
73 | _or1k_impure_ptr = _sbrk_r(0, sizeof(struct _reent*) * or1k_numcores()); |
---|
74 | _or1k_exception_impure_ptr = _sbrk_r(0, sizeof(struct _reent*) * or1k_numcores()); |
---|
75 | _or1k_current_impure_ptr = _sbrk_r(0, sizeof(struct _reent*) * or1k_numcores()); |
---|
76 | |
---|
77 | _or1k_impure_ptr[0] = _impure_ptr; |
---|
78 | _REENT_INIT_PTR(_impure_ptr); |
---|
79 | for (c = 1; c < or1k_numcores(); c++) { |
---|
80 | _or1k_impure_ptr[c] = _sbrk_r(0, sizeof(struct _reent)); |
---|
81 | _REENT_INIT_PTR(_or1k_impure_ptr[c]); |
---|
82 | } |
---|
83 | |
---|
84 | for (c = 0; c < or1k_numcores(); c++) { |
---|
85 | _or1k_exception_impure_ptr[c] = _sbrk_r(0, sizeof(struct _reent)); |
---|
86 | _REENT_INIT_PTR(_or1k_exception_impure_ptr[c]); |
---|
87 | } |
---|
88 | |
---|
89 | for (c = 0; c < or1k_numcores(); c++) { |
---|
90 | _or1k_current_impure_ptr[c] = _or1k_impure_ptr[c]; |
---|
91 | } |
---|
92 | #else |
---|
93 | // Initialize both impure data structures |
---|
94 | _REENT_INIT_PTR (_impure_ptr); |
---|
95 | _REENT_INIT_PTR (_or1k_exception_impure_ptr); |
---|
96 | |
---|
97 | // Use the standard impure ptr during normal software run |
---|
98 | _or1k_impure_ptr = _impure_ptr; |
---|
99 | |
---|
100 | // Set current to standard impure pointer |
---|
101 | _or1k_current_impure_ptr = _impure_ptr; |
---|
102 | #endif |
---|
103 | } |
---|
104 | |
---|
105 | struct _reent* |
---|
106 | _or1k_libc_getreent(void) { |
---|
107 | return OR1K_LIBC_GETREENT; |
---|
108 | } |
---|
109 | |
---|
110 | #ifdef __OR1K_MULTICORE__ |
---|
111 | struct _or1k_reent (*_or1k_reent)[]; |
---|
112 | #else |
---|
113 | struct _or1k_reent _or1k_reent; |
---|
114 | #endif |
---|
115 | |
---|
116 | void |
---|
117 | _or1k_reent_init(void) |
---|
118 | { |
---|
119 | #ifdef __OR1K_MULTICORE__ |
---|
120 | size_t memsize = sizeof(struct _or1k_reent) * or1k_numcores(); |
---|
121 | _or1k_reent = (struct _or1k_reent*) _sbrk_r(0, memsize); |
---|
122 | #endif |
---|
123 | } |
---|