1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) 2011, 2021 embedded brains GmbH & Co. KG |
---|
5 | * |
---|
6 | * Copyright (c) 2006 Kolja Waschk (rtemsdev/ixo.de) |
---|
7 | * |
---|
8 | * COPYRIGHT (c) 1989-2006 |
---|
9 | * On-Line Applications Research Corporation (OAR). |
---|
10 | * |
---|
11 | * Redistribution and use in source and binary forms, with or without |
---|
12 | * modification, are permitted provided that the following conditions |
---|
13 | * are met: |
---|
14 | * 1. Redistributions of source code must retain the above copyright |
---|
15 | * notice, this list of conditions and the following disclaimer. |
---|
16 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
17 | * notice, this list of conditions and the following disclaimer in the |
---|
18 | * documentation and/or other materials provided with the distribution. |
---|
19 | * |
---|
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
30 | * POSSIBILITY OF SUCH DAMAGE. |
---|
31 | */ |
---|
32 | |
---|
33 | #ifdef HAVE_CONFIG_H |
---|
34 | #include "config.h" |
---|
35 | #endif |
---|
36 | |
---|
37 | #include <string.h> |
---|
38 | |
---|
39 | #include <rtems/score/cpu.h> |
---|
40 | #include <rtems/score/nios2-utility.h> |
---|
41 | #include <rtems/score/interr.h> |
---|
42 | #include <rtems/score/tls.h> |
---|
43 | |
---|
44 | void _CPU_Context_Initialize( |
---|
45 | Context_Control *context, |
---|
46 | void *stack_area_begin, |
---|
47 | size_t stack_area_size, |
---|
48 | uint32_t new_level, |
---|
49 | void (*entry_point)( void ), |
---|
50 | bool is_fp, |
---|
51 | void *tls_area |
---|
52 | ) |
---|
53 | { |
---|
54 | const Nios2_MPU_Configuration *mpu_config = _Nios2_MPU_Get_configuration(); |
---|
55 | uint32_t stack = (uint32_t) stack_area_begin + stack_area_size - 4; |
---|
56 | |
---|
57 | memset(context, 0, sizeof(*context)); |
---|
58 | |
---|
59 | context->fp = stack; |
---|
60 | context->status = _Nios2_ISR_Set_level( new_level, NIOS2_STATUS_PIE ); |
---|
61 | context->sp = stack; |
---|
62 | context->ra = (uint32_t) entry_point; |
---|
63 | |
---|
64 | if ( mpu_config != NULL ) { |
---|
65 | Nios2_MPU_Region_descriptor desc = { |
---|
66 | .index = mpu_config->data_index_for_stack_protection, |
---|
67 | .base = stack_area_begin, |
---|
68 | .end = (const void *) RTEMS_ALIGN_UP( |
---|
69 | (uintptr_t) stack_area_begin + stack_area_size + |
---|
70 | _TLS_Get_allocation_size(), |
---|
71 | 1U << mpu_config->data_region_size_log2 |
---|
72 | ), |
---|
73 | .perm = NIOS2_MPU_DATA_PERM_SVR_READWRITE_USER_NONE, |
---|
74 | .data = true, |
---|
75 | .cacheable = mpu_config->enable_data_cache_for_stack, |
---|
76 | .read = false, |
---|
77 | .write = true |
---|
78 | }; |
---|
79 | bool ok = _Nios2_MPU_Setup_region_registers( |
---|
80 | mpu_config, |
---|
81 | &desc, |
---|
82 | &context->stack_mpubase, |
---|
83 | &context->stack_mpuacc |
---|
84 | ); |
---|
85 | |
---|
86 | if ( !ok ) { |
---|
87 | /* The task stack allocator must ensure that the stack area is valid */ |
---|
88 | _Terminate( INTERNAL_ERROR_CORE, 0xdeadbeef ); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | if ( tls_area != NULL ) { |
---|
93 | context->r23 = (uintptr_t) _TLS_Initialize_area( tls_area ) + 0x7000; |
---|
94 | } |
---|
95 | } |
---|