1 /* impure.c. Handling of re-entrancy data structure for OpenRISC 1000.
3 Copyright (C) 2014, Authors
5 Contributor Stefan Wallentowitz <stefan.wallentowitz@tum.de>
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
19 #include "or1k-internals.h"
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.
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.
33 * At runtime __getreent is called to return the current reentrancy pointer,
34 * which is stored in _current_impure_ptr.
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
41 /* Link in the external impure_data structure */
42 extern struct _reent
*__ATTRIBUTE_IMPURE_PTR__ _impure_ptr
;
44 #ifdef __OR1K_MULTICORE__
45 struct _reent
**_or1k_impure_ptr
;
46 struct _reent
**_or1k_exception_impure_ptr
;
47 struct _reent
**_or1k_current_impure_ptr
;
49 struct _reent
*__ATTRIBUTE_IMPURE_PTR__ _or1k_impure_ptr
;
51 /* Create exception impure data structure */
52 static struct _reent _or1k_exception_impure_data
= _REENT_INIT (_or1k_exception_impure_data
);
54 /* Link to the exception impure data structure */
55 struct _reent
*__ATTRIBUTE_IMPURE_PTR__ _or1k_exception_impure_ptr
= &_or1k_exception_impure_data
;
57 /* Link to the currently used data structure. */
58 struct _reent
*__ATTRIBUTE_IMPURE_PTR__ _or1k_current_impure_ptr
;
61 #ifdef __OR1K_MULTICORE__
62 #define OR1K_LIBC_GETREENT _or1k_current_impure_ptr[or1k_coreid()]
64 #define OR1K_LIBC_GETREENT _or1k_current_impure_ptr
68 _or1k_libc_impure_init (void)
70 #ifdef __OR1K_MULTICORE__
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());
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
]);
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
]);
89 for (c
= 0; c
< or1k_numcores(); c
++) {
90 _or1k_current_impure_ptr
[c
] = _or1k_impure_ptr
[c
];
93 // Initialize both impure data structures
94 _REENT_INIT_PTR (_impure_ptr
);
95 _REENT_INIT_PTR (_or1k_exception_impure_ptr
);
97 // Use the standard impure ptr during normal software run
98 _or1k_impure_ptr
= _impure_ptr
;
100 // Set current to standard impure pointer
101 _or1k_current_impure_ptr
= _impure_ptr
;
106 _or1k_libc_getreent(void) {
107 return OR1K_LIBC_GETREENT
;
110 #ifdef __OR1K_MULTICORE__
111 struct _or1k_reent (*_or1k_reent
)[];
113 struct _or1k_reent _or1k_reent
;
117 _or1k_reent_init(void)
119 #ifdef __OR1K_MULTICORE__
120 size_t memsize
= sizeof(struct _or1k_reent
) * or1k_numcores();
121 _or1k_reent
= (struct _or1k_reent
*) _sbrk_r(0, memsize
);