Skip to content

Commit ebc57ea

Browse files
aescolarnashif
authored andcommitted
native: Initial arch,soc & board to run Zephyr natively in a POSIX OS
A new arch (posix) which relies on pthreads to emulate the context switching A new soc for it (inf_clock) which emulates a CPU running at an infinely high clock (so when the CPU is awaken it runs till completion in 0 time) A new board, which provides a trivial system tick timer and irq generation. Note that this does not provide Kconfig and Makefiles to integrate with the default built system. All this is work in progress. The garbage/ folder is not meant to be merged ever. Origin: Original Fixes zephyrproject-rtos#1891 Signed-off-by: Alberto Escolar Piedras <[email protected]> Signed-off-by: Anas Nashif <[email protected]>
1 parent bce1b97 commit ebc57ea

33 files changed

+2023
-0
lines changed

arch/posix/core/cpuhalt.c

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2011-2015 Wind River Systems, Inc.
3+
* Copyright (c) 2017 Oticon A/S
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
/**
8+
* @file CPU power management code for POSIX
9+
*
10+
* This module provides an implementation of the architecture-specific
11+
* k_cpu_idle() primitive required by the kernel idle loop component.
12+
* It can be called within an implementation of _sys_power_save_idle(),
13+
* which is provided for the kernel by the platform.
14+
*
15+
* The module also provides an implementation of k_cpu_atomic_idle(), which
16+
* atomically re-enables interrupts and enters low power mode.
17+
*
18+
*/
19+
20+
#include <zephyr.h>
21+
#include "posix_core.h"
22+
23+
/**
24+
*
25+
* @brief Power save idle routine for IA-32
26+
*
27+
* This function will be called by the kernel idle loop or possibly within
28+
* an implementation of _sys_power_save_idle in the kernel when the
29+
* '_sys_power_save_flag' variable is non-zero.
30+
*
31+
* This function is just a pass thru to the SOC one
32+
*
33+
* @return N/A
34+
*/
35+
void k_cpu_idle(void)
36+
{
37+
posix_soc_halt_cpu();
38+
}
39+
40+
/**
41+
*
42+
* @brief Atomically re-enable interrupts and enter low power mode
43+
*
44+
* INTERNAL
45+
* The requirements for k_cpu_atomic_idle() are as follows:
46+
* 1) The enablement of interrupts and entering a low-power mode needs to be
47+
* atomic, i.e. there should be no period of time where interrupts are
48+
* enabled before the processor enters a low-power mode. See the comments
49+
* in k_lifo_get(), for example, of the race condition that
50+
* occurs if this requirement is not met.
51+
*
52+
* 2) After waking up from the low-power mode, the interrupt lockout state
53+
* must be restored as indicated in the 'imask' input parameter.
54+
*
55+
* This function is just a pass thru to the SOC one
56+
*
57+
* @return N/A
58+
*/
59+
60+
void k_cpu_atomic_idle(unsigned int imask)
61+
{
62+
posix_soc_atomic_halt_cpu(imask);
63+
}

arch/posix/core/fatal.c

+226
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation
3+
* Copyright (c) 2017 Oticon A/S
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
/*Started as a copy of the nios2 one with some prunning*/
9+
10+
#include <kernel.h>
11+
#include <arch/cpu.h>
12+
#include <kernel_structs.h>
13+
#include <misc/printk.h>
14+
#include <inttypes.h>
15+
16+
const NANO_ESF _default_esf = {
17+
0xdeadbaad
18+
};
19+
20+
/**
21+
*
22+
* @brief Kernel fatal error handler
23+
*
24+
* This routine is called when a fatal error condition is detected by either
25+
* hardware or software.
26+
*
27+
* The caller is expected to always provide a usable ESF. In the event that the
28+
* fatal error does not have a hardware generated ESF, the caller should either
29+
* create its own or call _Fault instead.
30+
*
31+
* @param reason the reason that the handler was called
32+
* @param pEsf pointer to the exception stack frame
33+
*
34+
* @return This function does not return.
35+
*/
36+
FUNC_NORETURN void _NanoFatalErrorHandler(unsigned int reason,
37+
const NANO_ESF *esf)
38+
{
39+
#ifdef CONFIG_PRINTK
40+
switch (reason) {
41+
case _NANO_ERR_CPU_EXCEPTION:
42+
case _NANO_ERR_SPURIOUS_INT:
43+
break;
44+
45+
case _NANO_ERR_INVALID_TASK_EXIT:
46+
printk("***** Invalid Exit Software Error! *****\n");
47+
break;
48+
49+
50+
case _NANO_ERR_ALLOCATION_FAIL:
51+
printk("**** Kernel Allocation Failure! ****\n");
52+
break;
53+
54+
case _NANO_ERR_KERNEL_OOPS:
55+
printk("***** Kernel OOPS! *****\n");
56+
break;
57+
58+
case _NANO_ERR_KERNEL_PANIC:
59+
printk("***** Kernel Panic! *****\n");
60+
break;
61+
62+
#ifdef CONFIG_STACK_SENTINEL
63+
case _NANO_ERR_STACK_CHK_FAIL:
64+
printk("***** Stack overflow *****\n");
65+
break;
66+
#endif
67+
default:
68+
printk("**** Unknown Fatal Error %u! ****\n", reason);
69+
break;
70+
}
71+
72+
#endif
73+
74+
void _SysFatalErrorHandler(unsigned int reason,
75+
const NANO_ESF *pEsf);
76+
_SysFatalErrorHandler(reason, esf);
77+
}
78+
79+
#if defined(CONFIG_EXTRA_EXCEPTION_INFO) && defined(CONFIG_PRINTK) \
80+
&& defined(ALT_CPU_HAS_EXTRA_EXCEPTION_INFO)
81+
static char *cause_str(u32_t cause_code)
82+
{
83+
switch (cause_code) {
84+
case 0:
85+
return "reset";
86+
case 1:
87+
return "processor-only reset request";
88+
case 2:
89+
return "interrupt";
90+
case 3:
91+
return "trap";
92+
case 4:
93+
return "unimplemented instruction";
94+
case 5:
95+
return "illegal instruction";
96+
case 6:
97+
return "misaligned data address";
98+
case 7:
99+
return "misaligned destination address";
100+
case 8:
101+
return "division error";
102+
case 9:
103+
return "supervisor-only instruction address";
104+
case 10:
105+
return "supervisor-only instruction";
106+
case 11:
107+
return "supervisor-only data address";
108+
case 12:
109+
return "TLB miss";
110+
case 13:
111+
return "TLB permission violation (execute)";
112+
case 14:
113+
return "TLB permission violation (read)";
114+
case 15:
115+
return "TLB permission violation (write)";
116+
case 16:
117+
return "MPU region violation (instruction)";
118+
case 17:
119+
return "MPU region violation (data)";
120+
case 18:
121+
return "ECC TLB error";
122+
case 19:
123+
return "ECC fetch error (instruction)";
124+
case 20:
125+
return "ECC register file error";
126+
case 21:
127+
return "ECC data error";
128+
case 22:
129+
return "ECC data cache writeback error";
130+
case 23:
131+
return "bus instruction fetch error";
132+
case 24:
133+
return "bus data region violation";
134+
default:
135+
return "unknown";
136+
}
137+
}
138+
#endif
139+
140+
FUNC_NORETURN void _Fault(const NANO_ESF *esf)
141+
{
142+
#ifdef CONFIG_PRINTK
143+
/* Unfortunately, completely unavailable on Nios II/e cores */
144+
#ifdef ALT_CPU_HAS_EXTRA_EXCEPTION_INFO
145+
u32_t exc_reg, badaddr_reg, eccftl;
146+
enum nios2_exception_cause cause;
147+
148+
exc_reg = _nios2_creg_read(NIOS2_CR_EXCEPTION);
149+
150+
/* Bit 31 indicates potentially fatal ECC error */
151+
eccftl = (exc_reg & NIOS2_EXCEPTION_REG_ECCFTL_MASK) != 0;
152+
153+
/* Bits 2-6 contain the cause code */
154+
cause = (exc_reg & NIOS2_EXCEPTION_REG_CAUSE_MASK)
155+
>> NIOS2_EXCEPTION_REG_CAUSE_OFST;
156+
157+
printk("Exception cause: %d ECCFTL: 0x%x\n", cause, eccftl);
158+
#if CONFIG_EXTRA_EXCEPTION_INFO
159+
printk("reason: %s\n", cause_str(cause));
160+
#endif
161+
if (BIT(cause) & NIOS2_BADADDR_CAUSE_MASK) {
162+
badaddr_reg = _nios2_creg_read(NIOS2_CR_BADADDR);
163+
printk("Badaddr: 0x%x\n", badaddr_reg);
164+
}
165+
#endif /* ALT_CPU_HAS_EXTRA_EXCEPTION_INFO */
166+
#endif /* CONFIG_PRINTK */
167+
168+
_NanoFatalErrorHandler(_NANO_ERR_CPU_EXCEPTION, esf);
169+
}
170+
171+
172+
/**
173+
*
174+
* @brief Fatal error handler
175+
*
176+
* This routine implements the corrective action to be taken when the system
177+
* detects a fatal error.
178+
*
179+
* This sample implementation attempts to abort the current thread and allow
180+
* the system to continue executing, which may permit the system to continue
181+
* functioning with degraded capabilities.
182+
*
183+
* System designers may wish to enhance or substitute this sample
184+
* implementation to take other actions, such as logging error (or debug)
185+
* information to a persistent repository and/or rebooting the system.
186+
*
187+
* @param reason the fatal error reason
188+
* @param pEsf pointer to exception stack frame
189+
*
190+
* @return N/A
191+
*/
192+
FUNC_NORETURN __weak void _SysFatalErrorHandler(unsigned int reason,
193+
const NANO_ESF *pEsf)
194+
{
195+
ARG_UNUSED(pEsf);
196+
197+
#if !defined(CONFIG_SIMPLE_FATAL_ERROR_HANDLER)
198+
#ifdef CONFIG_STACK_SENTINEL
199+
if (reason == _NANO_ERR_STACK_CHK_FAIL) {
200+
goto hang_system;
201+
}
202+
#endif
203+
if (reason == _NANO_ERR_KERNEL_PANIC) {
204+
goto hang_system;
205+
}
206+
if (k_is_in_isr() || _is_thread_essential()) {
207+
printk("Fatal fault in %s! Spinning...\n",
208+
k_is_in_isr() ? "ISR" : "essential thread");
209+
goto hang_system;
210+
}
211+
printk("Fatal fault in thread %p! Aborting.\n", _current);
212+
k_thread_abort(_current);
213+
214+
hang_system:
215+
#else
216+
ARG_UNUSED(reason);
217+
#endif
218+
219+
#ifdef ALT_CPU_HAS_DEBUG_STUB
220+
_nios2_break();
221+
#endif
222+
for (;;) {
223+
k_cpu_idle();
224+
}
225+
CODE_UNREACHABLE;
226+
}

0 commit comments

Comments
 (0)