commit ccb01550ccd794bb7c833ba144f45e374e9f0871
parent 87206e17ba053ec960830b253ec69db52b4ad3fe
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Wed, 4 Aug 2021 11:08:18 +0200
os9: Add support for kernel stack
The difference between user stack and kernel stack
was not clear and it was creating a problem, because
ictx() was trying to allocate a user space stack
in a task without user space enabled.
Change-Id: I40d0e387c19ceb8f20c9bd26ce218061cbe81e06
Diffstat:
5 files changed, 62 insertions(+), 42 deletions(-)
diff --git a/include/bits/arm64/arch/types.h b/include/bits/arm64/arch/types.h
@@ -39,15 +39,23 @@ enum regidx {
X27,
X28,
X29,
+
ELR,
X30,
+
SPSR,
ESR,
- SP,
+
+ SP_EL1,
+ SP_EL0,
+
FAR,
+ XZR,
+
TTBR0,
TTBR1,
- NR_REGS ,
+
+ NR_REGS,
};
typedef int atomic_t;
diff --git a/include/os9/os9.h b/include/os9/os9.h
@@ -241,6 +241,7 @@ struct task {
Context ctx;
Ptable ptable;
+ void *kstack;
/* communication ports */
/* healt monitor table */
/* Task *errhdl; */
diff --git a/src/os9/arch/arm64/arch.s b/src/os9/arch/arm64/arch.s
@@ -123,50 +123,56 @@ getctx:
MRS X10,ESR_EL1
STP X9,X10,[X0,#16*16]
- MOV X9,SP
- MRS X10,FAR_EL1
- STP X9,X10,[X0,#16*17]
+ MRS X9,SP_EL0
+ MRS X10,SP_EL1
+ STP X9,X10,[x0,#16*17]
+
+ MRS X9,FAR_EL1
+ STP X9,XZR,[x0,#16*18]
MRS X9,TTBR0_EL1
MRS X10,TTBR1_EL1
- STP X9,X10,[X0,#16*18]
+ STP X9,X10,[x0,#16*19]
RET
.TEXT
EXCEPTION:
- STP X0,X1,[SP,#-16*(19-0)]
- STP X2,X3,[SP,#-16*(19-1)]
- STP X4,X5,[SP,#-16*(19-2)]
- STP X6,X7,[SP,#-16*(19-3)]
- STP X8,X9,[SP,#-16*(19-4)]
- STP X10,X11,[SP,#-16*(19-5)]
- STP X12,X13,[SP,#-16*(19-6)]
- STP X14,X15,[SP,#-16*(19-7)]
- STP X16,X17,[SP,#-16*(19-8)]
- STP X18,X19,[SP,#-16*(19-9)]
- STP X20,X21,[SP,#-16*(19-10)]
- STP X22,X23,[SP,#-16*(19-11)]
- STP X24,X25,[SP,#-16*(19-12)]
- STP X26,X27,[SP,#-16*(19-13)]
- STP X28,X29,[SP,#-16*(19-14)]
+ STP X0,X1,[SP,#-16*(20-0)]
+ STP X2,X3,[SP,#-16*(20-1)]
+ STP X4,X5,[SP,#-16*(20-2)]
+ STP X6,X7,[SP,#-16*(20-3)]
+ STP X8,X9,[SP,#-16*(20-4)]
+ STP X10,X11,[SP,#-16*(20-5)]
+ STP X12,X13,[SP,#-16*(20-6)]
+ STP X14,X15,[SP,#-16*(20-7)]
+ STP X16,X17,[SP,#-16*(20-8)]
+ STP X18,X19,[SP,#-16*(20-9)]
+ STP X20,X21,[SP,#-16*(20-10)]
+ STP X22,X23,[SP,#-16*(20-11)]
+ STP X24,X25,[SP,#-16*(20-12)]
+ STP X26,X27,[SP,#-16*(20-13)]
+ STP X28,X29,[SP,#-16*(20-14)]
MRS X9,ELR_EL1
- STP X9,X30,[SP,#-16*(19-15)]
+ STP X9,X30,[SP,#-16*(20-15)]
MRS X9,SPSR_EL1
MRS X10,ESR_EL1
- STP X9,X10,[SP,#-16*(19-16)]
+ STP X9,X10,[SP,#-16*(20-16)]
+
+ MRS X9,SP_EL0
+ MRS X10,SP_EL1
+ STP X9,X10,[SP,#-16*(20-17)]
- MOV X9,SP
- MRS X10,FAR_EL1
- STP X9,X10,[SP,#-16*(19-17)]
+ MRS X9,FAR_EL1
+ STP X9,XZR,[SP,#-16*(20-18)]
MRS X9,TTBR0_EL1
MRS X10,TTBR1_EL1
- STP X9,X10,[SP,#-16*(19-18)]
+ STP X9,X10,[SP,#-16*(20-19)]
- SUB SP,SP,#16*19
+ SUB SP,SP,#16*20
MOV X0,SP
MOV X29,#0
BL trap
@@ -180,13 +186,16 @@ OUTSYN:
.TEXT
.GLOBL swtch
swtch:
- LDP X9,X10,[X0,#16*18]
+ LDP X9,X10,[X0,#16*19]
MSR TTBR0_EL1,X9
MSR TTBR1_EL1,X10
- LDP X9,X10,[X0,#16*17]
+ LDP X9,XZR,[X0,#16*18]
MSR FAR_EL1,X10
- MOV SP,X9
+
+ LDP X9,X10,[X0,#16*17]
+ MSR SP_EL0,X9
+ MSR SP_EL1,X10
LDP X9,X10,[X0,#16*16]
MSR ESR_EL1,X10
diff --git a/src/os9/arch/arm64/ctx.c b/src/os9/arch/arm64/ctx.c
@@ -41,7 +41,8 @@ static char regnames[][8] = {
[X30] = "x30",
[SPSR] = "spsr",
[ESR] = "esr",
- [SP] = "sp",
+ [SP_EL0] = "sp_el0",
+ [SP_EL1] = "sp_el1",
[FAR] = "far",
[TTBR0] = "ttbr0",
[TTBR1] = "ttbr1",
@@ -77,7 +78,7 @@ dumpstack(Context *ctx)
long long *sp, i;
kprint("stack dump:\n");
- sp = (long long *) ctx->r[SP];
+ sp = (long long *) ctx->r[SP_EL1];
for (i = 1; i <= 16; i++)
kprint("%0llx%c", *sp++, (i % 4 == 0) ? '\n' : ' ');
}
@@ -111,13 +112,11 @@ ictx(Task *tp, void *fn)
{
uint64_t *rp;
- if (!newstack(tp))
- return -1;
-
rp = tp->ctx.r;
memset(rp, 0, sizeof(tp->ctx.r));
+
rp[X30] = (uint64_t) outsync;
- rp[SP] = tp->stack.va;
+ rp[SP_EL1] = (uint64_t) tp->kstack;
rp[SPSR] = sysrd(SPSR_EL1);
rp[ELR] = (uint64_t) fn;
diff --git a/src/os9/proc.c b/src/os9/proc.c
@@ -454,12 +454,15 @@ kproc(void *fn)
tp->mode = PNORMAL;
tp->start = SKERNEL;
- tp->m = 0;
+ if ((tp->kstack = allocb(1)) == NULL)
+ goto err;
- if (ictx(tp, fn) < 0) {
- deltask(tp);
- return NULL;
- }
+ if (ictx(tp, fn) < 0)
+ goto err;
return tp;
+
+err:
+ deltask(tp);
+ return NULL;
}