Repository navigation
Expand file tree
/
Copy pathkernel.h
More file actions
162 lines (148 loc) · 5.14 KB
/
Copy pathkernel.h
File metadata and controls
162 lines (148 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
*
* - Added USER_BASE (0x1000000): base virtual address for user programs (must
* match user.ld).
* - Added SSTATUS_SPIE: enables interrupts for user mode.
*
* SYNTAX/CONCEPT NOTES:
* - #define USER_BASE 0x1000000 // must match user.ld
* - #define SSTATUS_SPIE (1 << 5) // enables interrupts for user mode
*
* Kernel Header File
*
* This header defines kernel-specific types, structures, and macros:
* 1. Process Management
* - Process states and limits
* - Process control structure
* - Process stack layout
*
* 2. Memory Management
* - Page table flags and permissions
* - Virtual memory configuration
* - Memory protection bits
*
* 3. System Interface
* - CSR (Control and Status Register) operations
* - SBI (Supervisor Binary Interface) structures
* - System call interface
*
* 4. System Control
* - Trap handling structures
* - System panic handling
* - Kernel initialization
*/
#pragma once
#include "common.h"
// The base virtual address of the application image.
// This address is supposed to match with the starting addresss
// mentioned in `user.ld`.
#define USER_BASE 0x1000000
#define SSTATUS_SPIE (1 << 5)
/*
* SCAUSE_ECALL is the trap cause code for ECALL (system call) from user mode.
* Used by the kernel to distinguish syscalls from other exceptions/faults.
*/
#define SCAUSE_ECALL 8
// Process Management
#define PROCS_MAX 8 // Maximum number of processes supported
#define PROC_EXITED 2 // Process has exited
#define PROC_UNUSED 0 // Process slot is free
#define PROC_RUNNABLE 1 // Process is ready to run
// System Interface
// CSR (Control and Status Register) operations
#define READ_CSR(reg) \
({ \
unsigned long __tmp; \
__asm__ __volatile__("csrr %0, " #reg : "=r"(__tmp)); \
__tmp; \
})
#define WRITE_CSR(reg, value) \
do { \
uint32_t __tmp = (value); \
__asm__ __volatile__("csrw " #reg ", %0" ::"r"(__tmp)); \
} while (0)
// Memory Management
// Page table configuration
#define SATP_SV32 (1u << 31) // Enable Sv32 paging mode
#define PAGE_V (1 << 0) // Page table entry valid bit
#define PAGE_R (1 << 1) // Page is readable
#define PAGE_W (1 << 2) // Page is writable
#define PAGE_X (1 << 3) // Page is executable
#define PAGE_U (1 << 4) // Page is user-accessible
// Page table index masks
#define TEN_ON_BITS 0x3ff // Mask for 10-bit page table indices
// System Control
// Trap handling structure - matches the register save order in kernel_entry
/*
* Trap frame structure: holds all CPU registers at the time of a trap/exception.
* Used by the kernel to access syscall arguments and return values.
*
* Syntax:
* __attribute__((packed)) ensures no padding between struct members.
*/
struct trap_frame {
uint32_t ra; // Return address
uint32_t gp; // Global pointer
uint32_t tp; // Thread pointer
uint32_t t0; // Temporary registers
uint32_t t1;
uint32_t t2;
uint32_t t3;
uint32_t t4;
uint32_t t5;
uint32_t t6;
uint32_t a0; // Argument registers
uint32_t a1;
uint32_t a2;
uint32_t a3;
uint32_t a4;
uint32_t a5;
uint32_t a6;
uint32_t a7;
uint32_t s0; // Saved registers
uint32_t s1;
uint32_t s2;
uint32_t s3;
uint32_t s4;
uint32_t s5;
uint32_t s6;
uint32_t s7;
uint32_t s8;
uint32_t s9;
uint32_t s10;
uint32_t s11;
uint32_t sp; // Stack pointer
} __attribute__((packed));
// Process Management
// Process control structure
struct process {
int pid; // Process identifier
int state; // Current process state
vaddr_t sp; // Stack pointer
uint32_t *page_table; // Process page table
uint8_t stack[8192]; // Process kernel stack
};
// System Interface
/*
* System call return value structure for SBI calls.
* Used for low-level hardware interface, not for user syscalls.
*/
struct ret_sbi {
long err; // Error code from SBI call
long val; // Return value from SBI call
};
// Function Declarations
// System Interface
struct ret_sbi call_sbi(long arg0, long arg1, long arg2, long arg3, long arg4,
long arg5, long fid, long eid); // Make SBI call
void putchar(char ch); // Output character
// System Control
void kernel_main(void); // Kernel entry point
// Error Handling
// PANIC macro for system errors - prints message and halts
#define PANIC(fmt, ...) \
do { \
printf("PANIC: %s:%d: " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
while (1) { \
} \
} while (0)