FreeNOS
ARM64Kernel.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 Ivan Tan
3 * Copyright (C) 2015 Niek Linnenbank
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <FreeNOS/System.h>
20#include <FreeNOS/ProcessManager.h>
21#include <SplitAllocator.h>
22#include <CoreInfo.h>
23#include <Log.h>
25#include <arm64/ARM64Constant.h>
26#include <arm64/ARM64Control.h>
27#include "ARM64Kernel.h"
28
29extern Address __start;
30
32{
33 FATAL("This is a test");
34}
35
37{
38 u64 ec = exception_code(state.esr);
39
40 switch(ec) {
41 case 0x25: //Data Abort
42 ERROR("Failed to access " << (void *)state.far);
43 break;
44 case 0x15:
45 trap(state);
46 default:
47 //omitted
48 break;
49 }
50}
51
53{
54 u64 ec = exception_code(state.esr);
55 NOTICE("Unexpected m_exception in EL1 called from EL1 ec="<<(void *)ec);
56 NOTICE("ESR_EL1 = "<<(void *)state.esr);
57
58 switch(ec) {
59 case 0x25: //Data Abort
60 ERROR("Failed to access " << (void *)state.far);
61 break;
62 case 0x15:
63 ERROR("SVC instruction execution in AArch64 state.");
64 default:
65 //omitted
66 break;
67 }
68 while(1) {};
69}
70
72 : Kernel(info)
73 , m_exception()
74{
75 NOTICE("");
76
77 // Setup interrupt callbacks
94
95 // First page is reserved
96 for (Size i = 0; i < info->kernel.phys; i += PAGESIZE)
97 m_alloc->allocate(i);
98
99 // Allocate physical memory for the temporary stack.
100 if (m_coreInfo->coreId == 0) {
101 for (Size i = 0; i < (PAGESIZE*16); i += PAGESIZE)
102 m_alloc->allocate(TMPSTACKADDR + i);
103 }
104}
105
106void ARM64Kernel::trap(volatile CPUState &state)
107{
109 ARM64Process *proc = (ARM64Process *) mgr->current(), *proc2;
110 ProcessID procId = proc->getID();
111
112 DEBUG("coreId = " << coreInfo.coreId << " procId = " << procId << " api = " << (Address)state.x8);
113 DEBUG("args = " << (void *)state.x0 << ", " << (void *)state.x1 << ", " << (void *)state.x2
114 << ", " << (void *)state.x3 << "," << (void *)state.x4);
115
116 // Execute the kernel call
118 (API::Number) state.x8,
119 state.x0,
120 state.x1,
121 state.x2,
122 state.x3,
123 state.x4
124 );
125
126 // Did we change process?
127 proc2 = (ARM64Process *) mgr->current();
128 DEBUG("result = " << r << " scheduled = " << (bool)(proc != proc2));
129
130 if (proc != proc2)
131 {
132 // Only if the previous process still exists (not killed in API)
133 if (mgr->get(procId) != NULL)
134 {
135 state.x0 = r;
136 proc->setCpuState((const CPUState *)&state);
137 }
138 MemoryBlock::copy((void*)&state, proc2->cpuState(), sizeof(state));
139 }
140 else
141 state.x0 = r;
142}
#define exception_code(esr)
Address __start
Result invoke(Number number, ulong arg1, ulong arg2, ulong arg3, ulong arg4, ulong arg5)
Execute a generic API function.
Definition API.cpp:35
Number
Enumeration of supported generic kernel API functions.
Definition API.h:50
Result install(ExceptionType vector, Handler handler)
Install an exception handler.
ARM64Kernel(CoreInfo *info)
Constructor function.
static void trap(volatile CPUState &state)
Software trap routine.
static void SyncExceptionEL0(volatile CPUState state)
Synchronous exceptions from EL0.
static void FatalHandler(volatile CPUState state)
Fatal errors.
static void SyncExceptionEL1(volatile CPUState state)
Synchronous exceptions from EL1.
ARM64Exception m_exception
ARM64 exception handling subsystem.
Definition ARM64Kernel.h:80
ARM64 specific process implementation.
void setCpuState(const CPUState *cpuState)
Overwrite the saved CPU registers for this task.
FreeNOS kernel implementation.
Definition Kernel.h:93
CoreInfo * m_coreInfo
CoreInfo object for this core.
Definition Kernel.h:236
SplitAllocator * m_alloc
Physical memory allocator.
Definition Kernel.h:227
API * getAPI()
Get API.
Definition Kernel.cpp:148
ProcessManager * getProcessManager()
Get process manager.
Definition Kernel.cpp:143
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
Represents a process which may run on the host.
Process * get(const ProcessID id)
Retrieve a Process by it's ID.
Process * current()
Current process running.
ProcessID getID() const
Retrieve our ID number.
Definition Process.cpp:60
virtual Result allocate(Range &args)
Allocate physical memory.
static Kernel * instance()
Retrieve the instance.
Definition Singleton.h:86
#define PAGESIZE
ARM uses 4K pages.
Definition ARMConstant.h:97
CoreInfo coreInfo
Local CoreInfo instance.
#define NULL
NULL means zero.
Definition Macros.h:39
u32 ProcessID
Process Identification Number.
Definition Types.h:140
unsigned int u32
Unsigned 32-bit number.
Definition Types.h:53
unsigned long Address
A memory address.
Definition Types.h:131
#define ERROR(msg)
Output an error message.
Definition Log.h:61
#define NOTICE(msg)
Output a notice message.
Definition Log.h:75
#define FATAL(msg)
Output a critical message and terminate program immediatly.
Definition Log.h:50
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
unsigned long long u64
Unsigned 64-bit number.
Definition Types.h:50
#define DEBUG(msg)
Output a debug message to standard output.
Definition Log.h:89
Contains all the CPU registers.
Definition ARMCore.h:244
Per-Core information structure.
Definition CoreInfo.h:61
Memory::Range kernel
Kernel memory range.
Definition CoreInfo.h:75
uint coreId
Core identifier.
Definition CoreInfo.h:66
Address phys
Physical address.
Definition Memory.h:58