mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
baremetal aarch64: create C version of multicore.S as well
Attempted to do the same for arm, but it failed.
This commit is contained in:
45
baremetal/arch/aarch64/multicore.c
Normal file
45
baremetal/arch/aarch64/multicore.c
Normal file
@@ -0,0 +1,45 @@
|
||||
/* https://cirosantilli.com/linux-kernel-module-cheat#arm-multicore
|
||||
*
|
||||
* Beware: things will blow up if the stack for CPU0 grow too much and
|
||||
* reaches that of CPU1. This is why it is so hard to do multithreading
|
||||
* without an OS that manages paging.
|
||||
*/
|
||||
|
||||
#include <lkmc.h>
|
||||
|
||||
uint64_t spinlock = 0;
|
||||
|
||||
__asm__(
|
||||
".text\n"
|
||||
".global lkmc_cpu_not_0\n"
|
||||
"lkmc_cpu_not_0:\n"
|
||||
/* Put all CPUs except CPU1 to sleep. */
|
||||
" cmp x0, 1\n"
|
||||
" bne .Lsleep_forever\n"
|
||||
/* Prepare the stack for CPU1. This is what we need
|
||||
* this assembly function for. */
|
||||
" ldr x0, =(stack_top - 0x1000)\n"
|
||||
" mov sp, x0\n"
|
||||
" bl main_cpu1\n"
|
||||
".Lsleep_forever:\n"
|
||||
" wfe\n"
|
||||
" b .Lsleep_forever\n"
|
||||
);
|
||||
|
||||
static void main_cpu1(void) {
|
||||
spinlock = 1;
|
||||
lkmc_arm_aarch64_dmb(sy);
|
||||
lkmc_arm_aarch64_sev();
|
||||
while (1) {
|
||||
lkmc_arm_aarch64_wfe();
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
#if !LKMC_GEM5
|
||||
lkmc_aarch64_psci_cpu_on(1, (uint64_t)main_cpu1, 0);
|
||||
#endif
|
||||
while (!spinlock) {
|
||||
lkmc_arm_aarch64_wfe();
|
||||
}
|
||||
}
|
||||
8
baremetal/arch/aarch64/no_bootloader/exit.S
Normal file
8
baremetal/arch/aarch64/no_bootloader/exit.S
Normal file
@@ -0,0 +1,8 @@
|
||||
/* Test _exit. */
|
||||
|
||||
#include <lkmc.h>
|
||||
|
||||
.global lkmc_start
|
||||
lkmc_start:
|
||||
mov x0, 0
|
||||
bl _exit
|
||||
@@ -1,11 +1,15 @@
|
||||
/* https://cirosantilli.com/linux-kernel-module-cheat#arm-multicore */
|
||||
/* https://cirosantilli.com/linux-kernel-module-cheat#arm-multicore
|
||||
*
|
||||
* This has to be in no_bootloader
|
||||
*/
|
||||
|
||||
#include <lkmc.h>
|
||||
|
||||
LKMC_PROLOGUE
|
||||
.global lkmc_start
|
||||
lkmc_start:
|
||||
/* Reset spinlock. */
|
||||
mov x0, 0
|
||||
ldr x1, =spinlock
|
||||
ldr x1, =.Lspinlock
|
||||
str x0, [x1]
|
||||
|
||||
/* Read cpu id into x1.
|
||||
@@ -14,11 +18,11 @@ LKMC_PROLOGUE
|
||||
*/
|
||||
mrs x1, mpidr_el1
|
||||
ands x1, x1, 3
|
||||
beq cpu0_only
|
||||
beq .Lcpu0_only
|
||||
.Lcpu1_only:
|
||||
/* Only CPU 1 reaches this point and sets the spinlock. */
|
||||
mov x0, 1
|
||||
ldr x1, =spinlock
|
||||
ldr x1, =.Lspinlock
|
||||
str x0, [x1]
|
||||
/* Ensure that CPU 0 sees the write right now.
|
||||
* Optional, but could save some useless CPU 1 loops.
|
||||
@@ -34,7 +38,7 @@ LKMC_PROLOGUE
|
||||
*/
|
||||
wfe
|
||||
b .Lcpu1_sleep_forever
|
||||
cpu0_only:
|
||||
.Lcpu0_only:
|
||||
/* Only CPU 0 reaches this point. */
|
||||
|
||||
#if !LKMC_GEM5
|
||||
@@ -61,11 +65,14 @@ cpu0_only:
|
||||
hvc 0
|
||||
#endif
|
||||
|
||||
spinlock_start:
|
||||
ldr x0, spinlock
|
||||
.Lspinlock_start:
|
||||
ldr x0, .Lspinlock
|
||||
/* Hint CPU 0 to enter low power mode. */
|
||||
wfe
|
||||
cbz x0, spinlock_start
|
||||
LKMC_EPILOGUE
|
||||
spinlock:
|
||||
cbz x0, .Lspinlock_start
|
||||
|
||||
mov x0, 0
|
||||
bl _exit
|
||||
|
||||
.Lspinlock:
|
||||
.skip 8
|
||||
@@ -1,15 +1,19 @@
|
||||
/* https://cirosantilli.com/linux-kernel-module-cheat#semihosting */
|
||||
/* https://cirosantilli.com/linux-kernel-module-cheat#semihosting
|
||||
*
|
||||
* Since our stack pointer is not setup, we justa allocate a memory
|
||||
* region to contain the semihosting arguments, which must be in memory.
|
||||
*/
|
||||
|
||||
.global lkmc_start
|
||||
lkmc_start:
|
||||
mov x1, 0x26
|
||||
movk x1, 2, lsl 16
|
||||
ldr x2, =semihost_args
|
||||
ldr x2, =.Lsemihost_args
|
||||
str x1, [x2, 0]
|
||||
mov x0, 0
|
||||
str x0, [x2, 8]
|
||||
mov x1, x2
|
||||
mov w0, 0x18
|
||||
hlt 0xf000
|
||||
semihost_args:
|
||||
.Lsemihost_args:
|
||||
.skip 16
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
/* https://cirosantilli.com/linux-kernel-module-cheat#semihosting */
|
||||
|
||||
.global main
|
||||
main:
|
||||
/* 0x20026 == ADP_Stopped_ApplicationExit */
|
||||
|
||||
@@ -71,7 +71,7 @@ int main(void) {
|
||||
printf("&after_svc %p\n", &&after_svc);
|
||||
assert(myvar == 0);
|
||||
/* Max 16-bits. */
|
||||
lkmc_svc(0xABCD);
|
||||
lkmc_arm_aarch64_svc(0xABCD);
|
||||
after_svc:
|
||||
assert(myvar == 1);
|
||||
return 0;
|
||||
|
||||
@@ -113,7 +113,7 @@ int main(void) {
|
||||
enable_irq();
|
||||
}
|
||||
while (1) {
|
||||
lkmc_wfi();
|
||||
lkmc_arm_aarch64_wfi();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
37
baremetal/arch/arm/multicore.c
Normal file
37
baremetal/arch/arm/multicore.c
Normal file
@@ -0,0 +1,37 @@
|
||||
/* https://cirosantilli.com/linux-kernel-module-cheat#arm-multicore */
|
||||
|
||||
#include <lkmc.h>
|
||||
|
||||
uint64_t spinlock = 0;
|
||||
|
||||
__asm__(
|
||||
".syntax unified\n"
|
||||
".text\n"
|
||||
".global lkmc_cpu_not_0\n"
|
||||
"lkmc_cpu_not_0:\n"
|
||||
" cmp r0, 1\n"
|
||||
" bne .Lsleep_forever\n"
|
||||
" ldr sp, =(stack_top - 0x1000)\n"
|
||||
" bl main_cpu1\n"
|
||||
".Lsleep_forever:\n"
|
||||
" wfe\n"
|
||||
" b .Lsleep_forever\n"
|
||||
);
|
||||
|
||||
static void main_cpu1(void) {
|
||||
spinlock = 1;
|
||||
lkmc_arm_aarch64_dmb(sy);
|
||||
lkmc_arm_aarch64_sev();
|
||||
while (1) {
|
||||
lkmc_arm_aarch64_wfe();
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
#if !LKMC_GEM5
|
||||
lkmc_arm_psci_cpu_on(1, (uint32_t)main_cpu1, 0);
|
||||
#endif
|
||||
while (!spinlock) {
|
||||
lkmc_arm_aarch64_wfe();
|
||||
}
|
||||
}
|
||||
@@ -2,24 +2,25 @@
|
||||
|
||||
#include <lkmc.h>
|
||||
|
||||
LKMC_PROLOGUE
|
||||
.global lkmc_start
|
||||
lkmc_start:
|
||||
mov r0, 0
|
||||
ldr r1, =spinlock
|
||||
ldr r1, =.Lspinlock
|
||||
str r0, [r1]
|
||||
/* Get CPU ID. */
|
||||
mrc p15, 0, r1, c0, c0, 5
|
||||
ands r1, r1, 3
|
||||
beq cpu0_only
|
||||
beq .Lcpu0_only
|
||||
.Lcpu1_only:
|
||||
mov r0, 1
|
||||
ldr r1, =spinlock
|
||||
ldr r1, =.Lspinlock
|
||||
str r0, [r1]
|
||||
dmb sy
|
||||
sev
|
||||
.Lcpu1_sleep_forever:
|
||||
wfe
|
||||
b .Lcpu1_sleep_forever
|
||||
cpu0_only:
|
||||
.Lcpu0_only:
|
||||
#if !LKMC_GEM5
|
||||
/* PSCI CPU_ON. */
|
||||
ldr r0, =0x84000003
|
||||
@@ -28,11 +29,12 @@ cpu0_only:
|
||||
mov r3, 0
|
||||
hvc 0
|
||||
#endif
|
||||
spinlock_start:
|
||||
ldr r0, spinlock
|
||||
.Lspinlock_start:
|
||||
ldr r0, .Lspinlock
|
||||
wfe
|
||||
cmp r0, 0
|
||||
beq spinlock_start
|
||||
LKMC_EPILOGUE
|
||||
spinlock:
|
||||
beq .Lspinlock_start
|
||||
mov r0, 0
|
||||
bl _exit
|
||||
.Lspinlock:
|
||||
.skip 4
|
||||
@@ -2,6 +2,11 @@
|
||||
|
||||
.global lkmc_start
|
||||
lkmc_start:
|
||||
/* Make all CPUs except CPU0 sleep by default. */
|
||||
mrs x0, mpidr_el1
|
||||
ands x0, x0, 3
|
||||
bne lkmc_cpu_not_0
|
||||
|
||||
/* Load the vector table. */
|
||||
ldr x0, =lkmc_vector_table
|
||||
msr vbar_el1, x0
|
||||
@@ -41,3 +46,8 @@ LKMC_WEAK(lkmc_vector_trap_handler)
|
||||
bl abort
|
||||
lkmc_vector_trap_handler_error_message:
|
||||
.asciz "error: unexpected interrupt"
|
||||
|
||||
/* Default action for CPUs besides the first one: sleep forever. */
|
||||
LKMC_WEAK(lkmc_cpu_not_0)
|
||||
wfe
|
||||
b lkmc_cpu_not_0
|
||||
|
||||
@@ -2,6 +2,11 @@
|
||||
|
||||
.global lkmc_start
|
||||
lkmc_start:
|
||||
/* Make all CPUs except CPU0 sleep by default. */
|
||||
mrc p15, 0, r0, c0, c0, 5
|
||||
ands r0, r0, 3
|
||||
bne lkmc_cpu_not_0
|
||||
|
||||
/* Prepare the stack for main, mandatory for C code. */
|
||||
ldr sp, =stack_top
|
||||
|
||||
@@ -28,3 +33,8 @@ lkmc_start:
|
||||
|
||||
/* If main returns, exit. */
|
||||
bl exit
|
||||
|
||||
/* Default action for CPUs besides the first one: sleep forever. */
|
||||
LKMC_WEAK(lkmc_cpu_not_0)
|
||||
wfe
|
||||
b lkmc_cpu_not_0
|
||||
|
||||
@@ -3,14 +3,6 @@
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <lkmc.h>
|
||||
#include <lkmc/m5ops.h>
|
||||
|
||||
void lkmc_baremetal_on_exit_callback(int status, void *arg) {
|
||||
(void)arg;
|
||||
if (status != 0) {
|
||||
printf("lkmc_exit_status_%d\n", status);
|
||||
}
|
||||
}
|
||||
|
||||
enum {
|
||||
UART_FR_RXFE = 0x10,
|
||||
@@ -19,44 +11,18 @@ enum {
|
||||
#define UART_DR(baseaddr) (*(unsigned int *)(baseaddr))
|
||||
#define UART_FR(baseaddr) (*(((unsigned int *)(baseaddr))+6))
|
||||
|
||||
void lkmc_baremetal_on_exit_callback(int status, void *arg) {
|
||||
(void)arg;
|
||||
if (status != 0) {
|
||||
printf("lkmc_exit_status_%d\n", status);
|
||||
}
|
||||
}
|
||||
|
||||
int _close(int file) {
|
||||
LKMC_UNUSED(file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
void _exit(int status) {
|
||||
LKMC_UNUSED(status);
|
||||
#if LKMC_GEM5
|
||||
LKMC_M5OPS_EXIT;
|
||||
#else
|
||||
#if defined(__arm__)
|
||||
__asm__ __volatile__ (
|
||||
"mov r0, #0x18\n"
|
||||
"ldr r1, =#0x20026\n"
|
||||
"svc 0x00123456\n"
|
||||
:
|
||||
:
|
||||
: "r0", "r1"
|
||||
);
|
||||
#elif defined(__aarch64__)
|
||||
/* TODO actually use the exit value here, just for fun. */
|
||||
__asm__ __volatile__ (
|
||||
"mov x1, #0x26\n" \
|
||||
"movk x1, #2, lsl #16\n" \
|
||||
"str x1, [sp,#0]\n" \
|
||||
"mov x0, #0\n" \
|
||||
"str x0, [sp,#8]\n" \
|
||||
"mov x1, sp\n" \
|
||||
"mov w0, #0x18\n" \
|
||||
"hlt 0xf000\n"
|
||||
:
|
||||
:
|
||||
: "x0", "x1"
|
||||
);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
int _fstat(int file, struct stat *st) {
|
||||
LKMC_UNUSED(file);
|
||||
st->st_mode = S_IFCHR;
|
||||
|
||||
35
baremetal/lib/syscalls_asm.S
Normal file
35
baremetal/lib/syscalls_asm.S
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <lkmc.h>
|
||||
|
||||
/* This is implemented in assembly so that it does not use the stack,
|
||||
* and thus can be called safely from programs without the bootloader.
|
||||
* C signature:
|
||||
*
|
||||
* void _exit(int status)
|
||||
*
|
||||
* If only there was a GCC attribute to create such a function!
|
||||
*/
|
||||
.text
|
||||
.global _exit
|
||||
_exit:
|
||||
#if LKMC_GEM5
|
||||
LKMC_M5OPS_EXIT_ASM
|
||||
#else
|
||||
/* Use semihosting:
|
||||
* https://github.com/cirosantilli/linux-kernel-module-cheat#semihosting */
|
||||
#if defined(__arm__)
|
||||
mov r0, #0x18
|
||||
ldr r1, =#0x20026
|
||||
svc 0x00123456
|
||||
#elif defined(__aarch64__)
|
||||
mov x1, 0x26
|
||||
movk x1, 2, lsl 16
|
||||
ldr x2, =.Lsemihost_args
|
||||
str x1, [x2, 0]
|
||||
str x0, [x2, 8]
|
||||
mov x1, x2
|
||||
mov w0, 0x18
|
||||
hlt 0xf000
|
||||
.Lsemihost_args:
|
||||
.skip 16
|
||||
#endif
|
||||
#endif
|
||||
Reference in New Issue
Block a user