Skip to content

Commit 600b5d8

Browse files
committed
checkpoint of tasks; gonna need cpu drivers
Signed-off-by: Amy Ringo <me@remexre.com>
1 parent c61d0c7 commit 600b5d8

File tree

8 files changed

+64
-8
lines changed

8 files changed

+64
-8
lines changed

src/kernel/arch/riscv64/hart_locals.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/*
2-
* SPDX-FileCopyrightText: 2025 ukoOS Contributors
2+
* SPDX-FileCopyrightText: 2025-2026 ukoOS Contributors
33
*
44
* SPDX-License-Identifier: GPL-3.0-or-later
55
*/
66

77
#include <arch/riscv64/insns.h>
88
#include <hart_locals.h>
9+
#include <mm/alloc.h>
10+
#include <task.h>
911

1012
/**
1113
* Storage for the boothart's hart-locals.
@@ -16,13 +18,24 @@ struct hart_locals *get_hart_locals(void) {
1618
return (struct hart_locals *)csrr(RISCV64_CSR_SSCRATCH);
1719
}
1820

19-
void init_boothart_hart_locals(u64 hart_id) {
21+
void init_boothart_hart_locals_early(u64 hart_id) {
2022
csrw(RISCV64_CSR_SSCRATCH, (u64)&boothart_hart_locals);
2123
boothart_hart_locals = (struct hart_locals){
2224
.hart_id = hart_id,
25+
.task = nullptr,
2326
.heap = nullptr,
2427
.rng = {},
2528
};
2629
}
2730

31+
void init_boothart_hart_locals_late(void) {
32+
// Reallocate the hart-locals to be heap-allocated.
33+
struct hart_locals *hart_locals = alloc(sizeof(struct hart_locals));
34+
memcpy(hart_locals, &boothart_hart_locals, sizeof(struct hart_locals));
35+
csrw(RISCV64_CSR_SSCRATCH, (u64)hart_locals);
36+
37+
// Create a task object for the current task.
38+
hart_locals->task = alloc(sizeof(struct task));
39+
}
40+
2841
void init_hart_locals(u64 hart_id);

src/kernel/arch/riscv64/include.mak

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ kernel-objs-c += arch/riscv64/panic
1515
kernel-objs-c += arch/riscv64/physical
1616
kernel-objs-c += arch/riscv64/random
1717
kernel-objs-c += arch/riscv64/sbi
18+
kernel-objs-c += arch/riscv64/task
1819

1920
$(eval $(call compute_component_variables,kernel))
2021

src/kernel/arch/riscv64/task.S

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2026 ukoOS Contributors
3+
*
4+
* SPDX-License-Identifier: GPL-3.0-or-later
5+
*/
6+
7+
.section .text
8+
9+
/**
10+
* Switches tasks.
11+
*/
12+
.p2align 2
13+
.global switch_to_task
14+
.type switch_to_task, @function
15+
switch_to_task:
16+
ebreak
17+
ret

src/kernel/include.mak

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ kernel-objs-c += random
4949
kernel-objs-c += selftest
5050
kernel-objs-c += swar_test
5151
kernel-objs-c += symbolicate
52-
kernel-objs-c += task
5352
include $(srcdir)/src/kernel/drivers/include.mak
5453

5554
# The architecture-specific file needs to be last, since it calls

src/kernel/include/hart_locals.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ struct hart_locals {
1818
*/
1919
u64 hart_id;
2020

21+
/**
22+
* The current task.
23+
*/
24+
struct task *task;
25+
2126
/**
2227
* The current heap.
2328
*/
@@ -29,6 +34,10 @@ struct hart_locals {
2934
struct random_rng rng;
3035
};
3136

37+
// Check that the layout that's assumed by assembly code.
38+
static_assert(offsetof(struct hart_locals, hart_id) == 0);
39+
static_assert(offsetof(struct hart_locals, task) == 8);
40+
3241
/**
3342
* Returns a pointer to the current hart's locals.
3443
*
@@ -42,7 +51,14 @@ struct hart_locals *get_hart_locals(void);
4251
* This needs to be separate from `init_hart_locals`, because the allocator
4352
* won't be available yet.
4453
*/
45-
void init_boothart_hart_locals(u64 hart_id);
54+
void init_boothart_hart_locals_early(u64 hart_id);
55+
56+
/**
57+
* Called during early boot to initialize the boothart's hart-local storage.
58+
* This needs to be separate from `init_hart_locals`, because the allocator
59+
* won't be available yet.
60+
*/
61+
void init_boothart_hart_locals_late(void);
4662

4763
/**
4864
* Called during hart bring-up to allocate hart-local storage for a hart.

src/kernel/include/task.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2025 ukoOS Contributors
2+
* SPDX-FileCopyrightText: 2025-2026 ukoOS Contributors
33
*
44
* SPDX-License-Identifier: GPL-3.0-or-later
55
*/
@@ -80,4 +80,14 @@ struct task {
8080
u8 register_save[];
8181
};
8282

83+
/**
84+
* Allocates a new task struct and initializes it.
85+
*/
86+
struct task *task_new(void);
87+
88+
/**
89+
* TODO: This should require that a hartlock be held.
90+
*/
91+
void switch_to_task(struct task *task);
92+
8393
#endif // UKO_OS_KERNEL__TASK_H

src/kernel/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2025 ukoOS Contributors
2+
* SPDX-FileCopyrightText: 2025-2026 ukoOS Contributors
33
*
44
* SPDX-License-Identifier: GPL-3.0-or-later
55
*/
@@ -25,8 +25,9 @@ void main(u64 hart_id, paddr devicetree_start, paddr kernel_start,
2525
symbolicate_init(symtab, symtab_len, strtab, strtab_len);
2626
entropy_pool_init();
2727
arch_entropy_pool_seed_early();
28-
init_boothart_hart_locals(hart_id);
28+
init_boothart_hart_locals_early(hart_id);
2929
mm_alloc_init();
30+
init_boothart_hart_locals_late();
3031

3132
// Parse the Devicetree.
3233
struct devicetree_node *devicetree = devicetree_parse_from_physical(

src/kernel/task.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
#include <task.h>

0 commit comments

Comments
 (0)