-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fiber] Add support for ARM64 targets
- Loading branch information
Showing
12 changed files
with
262 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright (c) 2020, Erik Henriksson | ||
* Copyright (c) 2021, 2023, Niklas Hauser | ||
* | ||
* This file is part of the modm project. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
// ---------------------------------------------------------------------------- | ||
|
||
#include "context.h" | ||
#include <modm/architecture/detect.hpp> | ||
|
||
/* Stack layout (growing downwards): | ||
* | ||
* Permanent Storage: | ||
* Fiber Function | ||
* Fiber Function Argument | ||
* | ||
* Temporary Prepare: | ||
* Entry Function | ||
* | ||
* Register file: | ||
* LR | ||
* FP | ||
* x28 | ||
* x27 | ||
* x26 | ||
* x25 | ||
* x24 | ||
* x23 | ||
* x22 | ||
* x21 | ||
* x20 | ||
* x19 | ||
* d15 | ||
* d14 | ||
* d13 | ||
* d12 | ||
* d11 | ||
* d10 | ||
* d9 | ||
* d8 | ||
*/ | ||
|
||
namespace | ||
{ | ||
|
||
constexpr size_t StackWordsReset = 1; | ||
constexpr size_t StackWordsStorage = 2; | ||
constexpr size_t StackWordsRegisters = 20; | ||
constexpr size_t StackWordsAll = StackWordsStorage + StackWordsRegisters; | ||
constexpr size_t StackSizeWord = sizeof(uintptr_t); | ||
constexpr uintptr_t StackWatermark = 0xc0ffee'f00d'facade; | ||
|
||
} | ||
|
||
extern "C" void modm_context_entry(); | ||
|
||
void | ||
modm_context_init(modm_context_t *ctx, | ||
uintptr_t *bottom, uintptr_t *top, | ||
uintptr_t fn, uintptr_t fn_arg) | ||
{ | ||
ctx->bottom = bottom; | ||
ctx->top = top; | ||
|
||
ctx->sp = top; | ||
*--ctx->sp = fn; | ||
*--ctx->sp = fn_arg; | ||
} | ||
|
||
void | ||
modm_context_reset(modm_context_t *ctx) | ||
{ | ||
*ctx->bottom = StackWatermark; | ||
|
||
ctx->sp = ctx->top - StackWordsStorage; | ||
*--ctx->sp = (uintptr_t) modm_context_entry; | ||
ctx->sp -= StackWordsRegisters - StackWordsReset; | ||
} | ||
|
||
void | ||
modm_context_watermark(modm_context_t *ctx) | ||
{ | ||
// clear the register file on the stack | ||
for (auto *word = ctx->top - StackWordsAll; | ||
word < ctx->top - StackWordsStorage - StackWordsReset; word++) | ||
*word = 0; | ||
|
||
// then color the whole stack *below* the register file | ||
for (auto *word = ctx->bottom; word < ctx->top - StackWordsAll; word++) | ||
*word = StackWatermark; | ||
} | ||
|
||
size_t | ||
modm_context_stack_usage(const modm_context_t *ctx) | ||
{ | ||
for (auto *word = ctx->bottom; word < ctx->top; word++) | ||
if (StackWatermark != *word) | ||
return (ctx->top - word) * StackSizeWord; | ||
return 0; | ||
} | ||
|
||
bool | ||
modm_context_stack_overflow(const modm_context_t *ctx) | ||
{ | ||
return *ctx->bottom != StackWatermark; | ||
} | ||
|
||
static modm_context_t main_context; | ||
|
||
void | ||
modm_context_start(modm_context_t *to) | ||
{ | ||
modm_context_jump(&main_context, to); | ||
} | ||
|
||
void | ||
modm_context_end() | ||
{ | ||
modm_context_t dummy; | ||
modm_context_jump(&dummy, &main_context); | ||
__builtin_unreachable(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (c) 2023, Niklas Hauser | ||
* | ||
* This file is part of the modm project. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
; ---------------------------------------------------------------------------- | ||
|
||
.text | ||
.globl _modm_context_entry | ||
.balign 16 | ||
_modm_context_entry: | ||
|
||
ldr x0, [sp] ; Load closure data pointer | ||
ldr x1, [sp, #8] ; Jump to closure function | ||
br x1 | ||
|
||
|
||
/* | ||
The assembly code below is adapted from the Boost Context library to work | ||
for Windows, Linux and macOS. | ||
See https://github.com/boostorg/context/tree/develop/src/asm | ||
- Windows: jump_arm64_aapcs_pe_armasm.asm | ||
- Linux: jump_arm64_aapcs_elf_gas.S | ||
- macOS: jump_arm64_aapcs_macho_gas.S | ||
|
||
Copyright Oliver Kowalke 2009. | ||
Distributed under the Boost Software License, Version 1.0. | ||
(See accompanying file LICENSE_1_0.txt or copy at | ||
http://www.boost.org/LICENSE_1_0.txt) | ||
*/ | ||
|
||
.text | ||
.globl _modm_context_jump | ||
.balign 16 | ||
_modm_context_jump: | ||
|
||
; move stack pointer down | ||
sub sp, sp, #0xa0 | ||
|
||
; save d8 - d15 | ||
stp d8, d9, [sp, #0x00] | ||
stp d10, d11, [sp, #0x10] | ||
stp d12, d13, [sp, #0x20] | ||
stp d14, d15, [sp, #0x30] | ||
|
||
; save x19-x30 | ||
stp x19, x20, [sp, #0x40] | ||
stp x21, x22, [sp, #0x50] | ||
stp x23, x24, [sp, #0x60] | ||
stp x25, x26, [sp, #0x70] | ||
stp x27, x28, [sp, #0x80] | ||
stp fp, lr, [sp, #0x90] | ||
|
||
; Store the SP in from->sp | ||
mov x19, sp | ||
str x19, [x0] | ||
|
||
; Restore SP from to->sp | ||
ldr x19, [x1] | ||
mov sp, x19 | ||
|
||
; load d8 - d15 | ||
ldp d8, d9, [sp, #0x00] | ||
ldp d10, d11, [sp, #0x10] | ||
ldp d12, d13, [sp, #0x20] | ||
ldp d14, d15, [sp, #0x30] | ||
|
||
; load x19-x30 | ||
ldp x19, x20, [sp, #0x40] | ||
ldp x21, x22, [sp, #0x50] | ||
ldp x23, x24, [sp, #0x60] | ||
ldp x25, x26, [sp, #0x70] | ||
ldp x27, x28, [sp, #0x80] | ||
ldp fp, lr, [sp, #0x90] | ||
|
||
; restore stack from GP + FPU | ||
add sp, sp, #0xa0 | ||
|
||
ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<!DOCTYPE rca SYSTEM "../devicefile.dtd"> | ||
<modm version="1.0"> | ||
<device platform="hosted" family="darwin" arch="arm64"> | ||
<naming-schema>{platform}-{family}-{arch}</naming-schema> | ||
|
||
<driver name="can" type="hosted"/> | ||
<driver name="can" type="canusb"/> | ||
<driver name="core" type="hosted-arm64"/> | ||
<driver name="gpio" type="hosted"/> | ||
<driver name="uart" type="hosted"/> | ||
</device> | ||
</modm> |
2 changes: 1 addition & 1 deletion
2
tools/devices/hosted/darwin.xml → tools/devices/hosted/darwin-x86_64.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<!DOCTYPE rca SYSTEM "../devicefile.dtd"> | ||
<modm version="1.0"> | ||
<device platform="hosted" family="linux" arch="arm64"> | ||
<naming-schema>{platform}-{family}-{arch}</naming-schema> | ||
|
||
<driver name="can" type="socketcan"/> | ||
<driver name="can" type="canusb"/> | ||
<driver name="core" type="hosted-x86_64"/> | ||
<driver name="graphics" type="hosted"/> | ||
<driver name="gpio" type="hosted"/> | ||
<driver name="uart" type="hosted"/> | ||
</device> | ||
</modm> |
2 changes: 1 addition & 1 deletion
2
tools/devices/hosted/linux.xml → tools/devices/hosted/linux-x86_64.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
tools/devices/hosted/rpi.xml → tools/devices/hosted/rpi-arm64.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters