1#![allow(dead_code)]
14#![deny(
16 clippy::unwrap_used,
17 clippy::expect_used,
18 clippy::panic,
19 clippy::indexing_slicing
20)]
21
22pub const SYS_YIELD: usize = 0;
25pub const SYS_EXIT: usize = 1;
27pub const SYS_PRINT: usize = 2;
29pub const SYS_READ_FILE: usize = 3;
31pub const SYS_WRITE_FILE: usize = 4;
33pub const SYS_WIN_CREATE: usize = 5;
36pub const SYS_WIN_BLIT: usize = 6;
38pub const SYS_WIN_POLL: usize = 7;
40pub const SYS_WIN_CLOSE: usize = 8;
42
43#[no_mangle]
56pub extern "C" fn rust_svc_handler(
57 x0: usize,
58 x1: usize,
59 _x2: usize,
60 syscall_nr: usize, ) -> usize {
62 match syscall_nr {
63 SYS_YIELD => {
64 crate::kernel::scheduler::yield_now();
66 0
67 }
68 SYS_EXIT => {
69 crate::info!("[SYS] Process exiting (exit code={})", x0);
71 crate::kernel::scheduler::exit();
72 }
74 SYS_PRINT => {
75 match crate::kernel::usercopy::user_str(x0, x1, 4095) {
78 Some(s) => crate::println!("[USER ] {}", s),
79 None => crate::error!("[SYS] sys_print: rejected (ptr=0x{:x}, len={})", x0, x1),
80 }
81 0
82 }
83 SYS_WIN_CREATE => crate::kernel::user_win::sys_win_create(x0),
84 SYS_WIN_BLIT => crate::kernel::user_win::sys_win_blit(x0, x1),
85 SYS_WIN_POLL => crate::kernel::user_win::sys_win_poll(x0, x1),
86 SYS_WIN_CLOSE => crate::kernel::user_win::sys_win_close(x0),
87 SYS_READ_FILE => {
88 crate::warn!("[SYS] sys_read_file (not yet implemented)");
91 usize::MAX }
93 SYS_WRITE_FILE => {
94 crate::warn!("[SYS] sys_write_file (not yet implemented)");
97 usize::MAX }
99 _ => {
100 crate::error!(
101 "[SYS] Unknown syscall number {} (x0=0x{:x})",
102 syscall_nr,
103 x0
104 );
105 usize::MAX }
107 }
108}
109
110#[no_mangle]
118pub extern "C" fn rust_data_abort_handler(far: usize, esr: usize, elr: usize) {
119 let ec = (esr >> 26) & 0x3F;
120 let iss = esr & 0x1FF_FFFF;
121 let fsc = iss & 0x3F; let wnr = (iss >> 6) & 1; let is_instruction_abort = ec == 0x20 || ec == 0x21;
125 let abort_type = if is_instruction_abort {
126 "INSTRUCTION ABORT"
127 } else {
128 "DATA ABORT"
129 };
130
131 let core = crate::kernel::scheduler::core_id();
132 let pid = crate::kernel::scheduler::get_current_process_id().unwrap_or(0);
133 let proc_name = crate::kernel::scheduler::get_current_process_name().unwrap_or("unknown");
134 crate::kernel::uart::begin_fault_output();
135
136 crate::error!(
137 "[SYS] === {} from EL0 (Core {}, Process: '{}', PID: {}) ===",
138 abort_type,
139 core,
140 proc_name,
141 pid
142 );
143 crate::error!("[SYS] ELR_EL1 (Fault PC): 0x{:016x}", elr);
144 crate::error!("[SYS] FAR_EL1 (Fault Address): 0x{:016x}", far);
145 crate::error!(
146 "[SYS] ESR_EL1: 0x{:08x} (EC: 0x{:02x}, ISS: 0x{:06x})",
147 esr,
148 ec,
149 iss
150 );
151
152 if is_instruction_abort {
153 crate::error!("[SYS] IFSC: 0x{:02x}", fsc);
154 } else {
155 crate::error!(
156 "[SYS] DFSC: 0x{:02x}, WnR: {} ({})",
157 fsc,
158 wnr,
159 if wnr == 1 { "Write" } else { "Read" }
160 );
161 }
162
163 if far >= 0x3C00_0000 {
164 crate::error!("[SYS] -> MMIO region access violation! Terminating process.");
165 } else {
166 crate::error!("[SYS] -> Memory access violation! Terminating process.");
167 }
168
169 crate::kernel::uart::end_fault_output();
171 crate::kernel::scheduler::exit();
172}
173
174#[no_mangle]
185pub unsafe extern "C" fn rust_el1_sync_handler(far: usize, esr: usize, tf: *const usize) {
186 let ec = (esr >> 26) & 0x3F;
187 let iss = esr & 0x1FFFFFF;
188
189 let core = crate::kernel::scheduler::core_id();
190 let pid = crate::kernel::scheduler::get_current_process_id().unwrap_or(0);
191 let proc_name = crate::kernel::scheduler::get_current_process_name().unwrap_or("kernel/init");
192 crate::kernel::uart::begin_fault_output();
193
194 crate::println!("==================================================");
195 crate::println!("!!! KERNEL PANIC: EL1 Synchronous Exception !!!");
196 crate::println!(" Core: {}, Process: '{}' (PID: {})", core, proc_name, pid);
197 crate::println!("==================================================");
198 crate::println!(" FAR_EL1 (fault address): 0x{:016x}", far);
199 crate::println!(
200 " ESR_EL1 (exception reg): 0x{:08x} (EC: 0x{:02x}, ISS: 0x{:06x})",
201 esr,
202 ec,
203 iss
204 );
205
206 let elr = *tf.add(32);
209 let spsr = *tf.add(33);
210 crate::println!(" ELR_EL1 (Exception PC): 0x{:016x}", elr);
211 crate::println!(" SPSR_EL1 (saved state): 0x{:08x}", spsr);
212
213 crate::println!(" Registers:");
215 for i in 0..16 {
216 let val1 = *tf.add(i);
217 let val2 = *tf.add(i + 16);
218 if i + 16 < 31 {
219 crate::println!(
220 " x{:02}: 0x{:016x} x{:02}: 0x{:016x}",
221 i,
222 val1,
223 i + 16,
224 val2
225 );
226 } else {
227 crate::println!(" x{:02}: 0x{:016x}", i, val1);
228 }
229 }
230
231 loop {
232 unsafe {
233 core::arch::asm!("wfe");
234 }
235 }
236}