1#![no_std]
8#![no_main]
9
10use capsules_core::virtualizers::virtual_alarm::{MuxAlarm, VirtualMuxAlarm};
11use kernel::capabilities;
12use kernel::component::Component;
13use kernel::hil::led::LedHigh;
14use kernel::hil::time::{Alarm, Timer};
15use kernel::platform::chip::InterruptService;
16use kernel::platform::scheduler_timer::VirtualSchedulerTimer;
17use kernel::platform::{KernelResources, SyscallDriverLookup};
18use kernel::process::ProcessArray;
19use kernel::scheduler::mlfq::MLFQSched;
20use kernel::utilities::registers::interfaces::ReadWriteable;
21use kernel::utilities::StaticRef;
22use kernel::{create_capability, debug, static_init};
23use rv32i::csr;
24
25mod io;
26mod litex_generated_constants;
27
28use litex_generated_constants as socc;
35
36struct LiteXSimInterruptablePeripherals {
44 gpio0: &'static litex_vexriscv::gpio::LiteXGPIOController<'static, socc::SoCRegisterFmt>,
45 uart0: &'static litex_vexriscv::uart::LiteXUart<'static, socc::SoCRegisterFmt>,
46 timer0: &'static litex_vexriscv::timer::LiteXTimer<
47 'static,
48 socc::SoCRegisterFmt,
49 socc::ClockFrequency,
50 >,
51 ethmac0: &'static litex_vexriscv::liteeth::LiteEth<
52 'static,
53 { socc::ETHMAC_TX_SLOTS },
54 socc::SoCRegisterFmt,
55 >,
56}
57
58impl LiteXSimInterruptablePeripherals {
59 pub fn init(&'static self) {
61 kernel::deferred_call::DeferredCallClient::register(self.uart0);
62 }
63}
64
65impl InterruptService for LiteXSimInterruptablePeripherals {
66 unsafe fn service_interrupt(&self, interrupt: u32) -> bool {
67 match interrupt as usize {
68 socc::UART_INTERRUPT => {
69 self.uart0.service_interrupt();
70 true
71 }
72 socc::TIMER0_INTERRUPT => {
73 self.timer0.service_interrupt();
74 true
75 }
76 socc::ETHMAC_INTERRUPT => {
77 self.ethmac0.service_interrupt();
78 true
79 }
80 socc::GPIO_INTERRUPT => {
81 self.gpio0.service_interrupt();
82 true
83 }
84 _ => false,
85 }
86 }
87}
88
89const NUM_PROCS: usize = 4;
90
91static mut PROCESSES: Option<&'static ProcessArray<NUM_PROCS>> = None;
93
94struct LiteXSimPanicReferences {
96 chip: Option<&'static litex_vexriscv::chip::LiteXVexRiscv<LiteXSimInterruptablePeripherals>>,
97 uart: Option<&'static litex_vexriscv::uart::LiteXUart<'static, socc::SoCRegisterFmt>>,
98 process_printer: Option<&'static capsules_system::process_printer::ProcessPrinterText>,
99}
100static mut PANIC_REFERENCES: LiteXSimPanicReferences = LiteXSimPanicReferences {
101 chip: None,
102 uart: None,
103 process_printer: None,
104};
105
106const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
108 capsules_system::process_policies::PanicFaultPolicy {};
109
110kernel::stack_size! {0x2000}
111
112struct LiteXSim {
115 gpio_driver: &'static capsules_core::gpio::GPIO<
116 'static,
117 litex_vexriscv::gpio::LiteXGPIOPin<'static, 'static, socc::SoCRegisterFmt>,
118 >,
119 button_driver: &'static capsules_core::button::Button<
120 'static,
121 litex_vexriscv::gpio::LiteXGPIOPin<'static, 'static, socc::SoCRegisterFmt>,
122 >,
123 led_driver: &'static capsules_core::led::LedDriver<
124 'static,
125 LedHigh<
126 'static,
127 litex_vexriscv::gpio::LiteXGPIOPin<'static, 'static, socc::SoCRegisterFmt>,
128 >,
129 8,
130 >,
131 console: &'static capsules_core::console::Console<'static>,
132 lldb: &'static capsules_core::low_level_debug::LowLevelDebug<
133 'static,
134 capsules_core::virtualizers::virtual_uart::UartDevice<'static>,
135 >,
136 alarm: &'static capsules_core::alarm::AlarmDriver<
137 'static,
138 VirtualMuxAlarm<
139 'static,
140 litex_vexriscv::timer::LiteXAlarm<
141 'static,
142 'static,
143 socc::SoCRegisterFmt,
144 socc::ClockFrequency,
145 >,
146 >,
147 >,
148 ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
149 scheduler: &'static MLFQSched<
150 'static,
151 VirtualMuxAlarm<
152 'static,
153 litex_vexriscv::timer::LiteXAlarm<
154 'static,
155 'static,
156 socc::SoCRegisterFmt,
157 socc::ClockFrequency,
158 >,
159 >,
160 >,
161 scheduler_timer: &'static VirtualSchedulerTimer<
162 VirtualMuxAlarm<
163 'static,
164 litex_vexriscv::timer::LiteXAlarm<
165 'static,
166 'static,
167 socc::SoCRegisterFmt,
168 socc::ClockFrequency,
169 >,
170 >,
171 >,
172}
173
174impl SyscallDriverLookup for LiteXSim {
176 fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
177 where
178 F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
179 {
180 match driver_num {
181 capsules_core::button::DRIVER_NUM => f(Some(self.button_driver)),
182 capsules_core::led::DRIVER_NUM => f(Some(self.led_driver)),
183 capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio_driver)),
184 capsules_core::console::DRIVER_NUM => f(Some(self.console)),
185 capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
186 capsules_core::low_level_debug::DRIVER_NUM => f(Some(self.lldb)),
187 kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
188 _ => f(None),
189 }
190 }
191}
192
193impl KernelResources<litex_vexriscv::chip::LiteXVexRiscv<LiteXSimInterruptablePeripherals>>
194 for LiteXSim
195{
196 type SyscallDriverLookup = Self;
197 type SyscallFilter = ();
198 type ProcessFault = ();
199 type Scheduler = MLFQSched<
200 'static,
201 VirtualMuxAlarm<
202 'static,
203 litex_vexriscv::timer::LiteXAlarm<
204 'static,
205 'static,
206 socc::SoCRegisterFmt,
207 socc::ClockFrequency,
208 >,
209 >,
210 >;
211 type SchedulerTimer = VirtualSchedulerTimer<
212 VirtualMuxAlarm<
213 'static,
214 litex_vexriscv::timer::LiteXAlarm<
215 'static,
216 'static,
217 socc::SoCRegisterFmt,
218 socc::ClockFrequency,
219 >,
220 >,
221 >;
222 type WatchDog = ();
223 type ContextSwitchCallback = ();
224
225 fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
226 self
227 }
228 fn syscall_filter(&self) -> &Self::SyscallFilter {
229 &()
230 }
231 fn process_fault(&self) -> &Self::ProcessFault {
232 &()
233 }
234 fn scheduler(&self) -> &Self::Scheduler {
235 self.scheduler
236 }
237 fn scheduler_timer(&self) -> &Self::SchedulerTimer {
238 self.scheduler_timer
239 }
240 fn watchdog(&self) -> &Self::WatchDog {
241 &()
242 }
243 fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
244 &()
245 }
246}
247
248#[inline(never)]
252unsafe fn start() -> (
253 &'static kernel::Kernel,
254 LiteXSim,
255 &'static litex_vexriscv::chip::LiteXVexRiscv<LiteXSimInterruptablePeripherals>,
256) {
257 extern "C" {
259 static _sapps: u8;
261 static _eapps: u8;
263 static mut _sappmem: u8;
265 static _eappmem: u8;
267 static _stext: u8;
269 static _etext: u8;
271 static _sflash: u8;
273 static _eflash: u8;
275 static _ssram: u8;
277 static _esram: u8;
279 }
280
281 rv32i::configure_trap_handler();
285
286 let pmp = rv32i::pmp::kernel_protection::KernelProtectionPMP::new(
290 rv32i::pmp::kernel_protection::FlashRegion(
291 rv32i::pmp::NAPOTRegionSpec::from_start_end(
292 core::ptr::addr_of!(_sflash),
293 core::ptr::addr_of!(_eflash),
294 )
295 .unwrap(),
296 ),
297 rv32i::pmp::kernel_protection::RAMRegion(
298 rv32i::pmp::NAPOTRegionSpec::from_start_end(
299 core::ptr::addr_of!(_ssram),
300 core::ptr::addr_of!(_esram),
301 )
302 .unwrap(),
303 ),
304 rv32i::pmp::kernel_protection::MMIORegion(
305 rv32i::pmp::NAPOTRegionSpec::from_start_size(
306 0xf0000000 as *const u8, 0x10000000, )
309 .unwrap(),
310 ),
311 rv32i::pmp::kernel_protection::KernelTextRegion(
312 rv32i::pmp::TORRegionSpec::from_start_end(
313 core::ptr::addr_of!(_stext),
314 core::ptr::addr_of!(_etext),
315 )
316 .unwrap(),
317 ),
318 )
319 .unwrap();
320
321 let process_mgmt_cap = create_capability!(capabilities::ProcessManagementCapability);
323 let memory_allocation_cap = create_capability!(capabilities::MemoryAllocationCapability);
324
325 let processes = components::process_array::ProcessArrayComponent::new()
327 .finalize(components::process_array_component_static!(NUM_PROCS));
328 PROCESSES = Some(processes);
329
330 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
332
333 let timer0 = static_init!(
337 litex_vexriscv::timer::LiteXTimer<'static, socc::SoCRegisterFmt, socc::ClockFrequency>,
338 litex_vexriscv::timer::LiteXTimer::new(StaticRef::new(
339 socc::CSR_TIMER0_BASE
340 as *const litex_vexriscv::timer::LiteXTimerRegisters<socc::SoCRegisterFmt>
341 ),)
342 );
343
344 let timer0_uptime = static_init!(
346 litex_vexriscv::timer::LiteXTimerUptime<
347 'static,
348 socc::SoCRegisterFmt,
349 socc::ClockFrequency,
350 >,
351 litex_vexriscv::timer::LiteXTimerUptime::new(timer0)
352 );
353
354 let litex_alarm = static_init!(
357 litex_vexriscv::timer::LiteXAlarm<
358 'static,
359 'static,
360 socc::SoCRegisterFmt,
361 socc::ClockFrequency,
362 >,
363 litex_vexriscv::timer::LiteXAlarm::new(timer0_uptime, timer0)
364 );
365 timer0.set_timer_client(litex_alarm);
366 litex_alarm.initialize();
367
368 let mux_alarm = static_init!(
371 MuxAlarm<
372 'static,
373 litex_vexriscv::timer::LiteXAlarm<
374 'static,
375 'static,
376 socc::SoCRegisterFmt,
377 socc::ClockFrequency,
378 >,
379 >,
380 MuxAlarm::new(litex_alarm)
381 );
382 litex_alarm.set_alarm_client(mux_alarm);
383
384 let virtual_alarm_user = static_init!(
386 VirtualMuxAlarm<
387 'static,
388 litex_vexriscv::timer::LiteXAlarm<
389 'static,
390 'static,
391 socc::SoCRegisterFmt,
392 socc::ClockFrequency,
393 >,
394 >,
395 VirtualMuxAlarm::new(mux_alarm)
396 );
397 virtual_alarm_user.setup();
398
399 let alarm = static_init!(
400 capsules_core::alarm::AlarmDriver<
401 'static,
402 VirtualMuxAlarm<
403 'static,
404 litex_vexriscv::timer::LiteXAlarm<
405 'static,
406 'static,
407 socc::SoCRegisterFmt,
408 socc::ClockFrequency,
409 >,
410 >,
411 >,
412 capsules_core::alarm::AlarmDriver::new(
413 virtual_alarm_user,
414 board_kernel.create_grant(capsules_core::alarm::DRIVER_NUM, &memory_allocation_cap)
415 )
416 );
417 virtual_alarm_user.set_alarm_client(alarm);
418
419 let systick_virtual_alarm = static_init!(
421 VirtualMuxAlarm<
422 'static,
423 litex_vexriscv::timer::LiteXAlarm<
424 'static,
425 'static,
426 socc::SoCRegisterFmt,
427 socc::ClockFrequency,
428 >,
429 >,
430 VirtualMuxAlarm::new(mux_alarm)
431 );
432 systick_virtual_alarm.setup();
433
434 let scheduler_timer = static_init!(
435 VirtualSchedulerTimer<
436 VirtualMuxAlarm<
437 'static,
438 litex_vexriscv::timer::LiteXAlarm<
439 'static,
440 'static,
441 socc::SoCRegisterFmt,
442 socc::ClockFrequency,
443 >,
444 >,
445 >,
446 VirtualSchedulerTimer::new(systick_virtual_alarm)
447 );
448
449 let uart0 = static_init!(
453 litex_vexriscv::uart::LiteXUart<socc::SoCRegisterFmt>,
454 litex_vexriscv::uart::LiteXUart::new(
455 StaticRef::new(
456 socc::CSR_UART_BASE
457 as *const litex_vexriscv::uart::LiteXUartRegisters<socc::SoCRegisterFmt>,
458 ),
459 None, )
461 );
462 uart0.initialize();
463
464 PANIC_REFERENCES.uart = Some(uart0);
465
466 let uart_mux = components::console::UartMuxComponent::new(uart0, 115200)
471 .finalize(components::uart_mux_component_static!());
472
473 let ethmac0 = static_init!(
477 litex_vexriscv::liteeth::LiteEth<{socc::ETHMAC_TX_SLOTS}, socc::SoCRegisterFmt>,
478 litex_vexriscv::liteeth::LiteEth::new(
479 StaticRef::new(
480 socc::CSR_ETHMAC_BASE
481 as *const litex_vexriscv::liteeth::LiteEthMacRegisters<socc::SoCRegisterFmt>,
482 ),
483 socc::MEM_ETHMAC_BASE,
484 socc::MEM_ETHMAC_SIZE,
485 socc::ETHMAC_SLOT_SIZE,
486 socc::ETHMAC_RX_SLOTS,
487 socc::ETHMAC_TX_SLOTS,
488 )
489 );
490
491 ethmac0.initialize();
493
494 type GPIOPin = litex_vexriscv::gpio::LiteXGPIOPin<'static, 'static, socc::SoCRegisterFmt>;
496
497 let gpio0 = static_init!(
499 litex_vexriscv::gpio::LiteXGPIOController<'static, socc::SoCRegisterFmt>,
500 litex_vexriscv::gpio::LiteXGPIOController::new(
501 StaticRef::new(
502 socc::CSR_GPIO_BASE
503 as *const litex_vexriscv::gpio::LiteXGPIORegisters<socc::SoCRegisterFmt>
504 ),
505 32, ),
507 );
508 gpio0.initialize();
509
510 let gpio_driver = components::gpio::GpioComponent::new(
513 board_kernel,
514 capsules_core::gpio::DRIVER_NUM,
515 components::gpio_component_helper_owned!(
516 GPIOPin,
517 16 => gpio0.get_gpio_pin(16).unwrap(),
518 17 => gpio0.get_gpio_pin(17).unwrap(),
519 18 => gpio0.get_gpio_pin(18).unwrap(),
520 19 => gpio0.get_gpio_pin(19).unwrap(),
521 20 => gpio0.get_gpio_pin(20).unwrap(),
522 21 => gpio0.get_gpio_pin(21).unwrap(),
523 22 => gpio0.get_gpio_pin(22).unwrap(),
524 23 => gpio0.get_gpio_pin(23).unwrap(),
525 24 => gpio0.get_gpio_pin(24).unwrap(),
526 25 => gpio0.get_gpio_pin(25).unwrap(),
527 26 => gpio0.get_gpio_pin(26).unwrap(),
528 27 => gpio0.get_gpio_pin(27).unwrap(),
529 28 => gpio0.get_gpio_pin(28).unwrap(),
530 29 => gpio0.get_gpio_pin(29).unwrap(),
531 30 => gpio0.get_gpio_pin(30).unwrap(),
532 31 => gpio0.get_gpio_pin(31).unwrap(),
533 ),
534 )
535 .finalize(components::gpio_component_static!(GPIOPin));
536
537 let led_gpios = static_init!(
540 [GPIOPin; 8],
541 [
542 gpio0.get_gpio_pin(0).unwrap(),
543 gpio0.get_gpio_pin(1).unwrap(),
544 gpio0.get_gpio_pin(2).unwrap(),
545 gpio0.get_gpio_pin(3).unwrap(),
546 gpio0.get_gpio_pin(4).unwrap(),
547 gpio0.get_gpio_pin(5).unwrap(),
548 gpio0.get_gpio_pin(6).unwrap(),
549 gpio0.get_gpio_pin(7).unwrap(),
550 ]
551 );
552
553 let led_driver =
554 components::led::LedsComponent::new().finalize(components::led_component_static!(
555 kernel::hil::led::LedHigh<GPIOPin>,
556 LedHigh::new(&led_gpios[0]),
557 LedHigh::new(&led_gpios[1]),
558 LedHigh::new(&led_gpios[2]),
559 LedHigh::new(&led_gpios[3]),
560 LedHigh::new(&led_gpios[4]),
561 LedHigh::new(&led_gpios[5]),
562 LedHigh::new(&led_gpios[6]),
563 LedHigh::new(&led_gpios[7]),
564 ));
565
566 let button_driver = components::button::ButtonComponent::new(
569 board_kernel,
570 capsules_core::button::DRIVER_NUM,
571 components::button_component_helper_owned!(
572 GPIOPin,
573 (
574 gpio0.get_gpio_pin(8).unwrap(),
575 kernel::hil::gpio::ActivationMode::ActiveHigh,
576 kernel::hil::gpio::FloatingState::PullNone
577 ),
578 (
579 gpio0.get_gpio_pin(9).unwrap(),
580 kernel::hil::gpio::ActivationMode::ActiveHigh,
581 kernel::hil::gpio::FloatingState::PullNone
582 ),
583 (
584 gpio0.get_gpio_pin(10).unwrap(),
585 kernel::hil::gpio::ActivationMode::ActiveHigh,
586 kernel::hil::gpio::FloatingState::PullNone
587 ),
588 (
589 gpio0.get_gpio_pin(11).unwrap(),
590 kernel::hil::gpio::ActivationMode::ActiveHigh,
591 kernel::hil::gpio::FloatingState::PullNone
592 ),
593 (
594 gpio0.get_gpio_pin(12).unwrap(),
595 kernel::hil::gpio::ActivationMode::ActiveHigh,
596 kernel::hil::gpio::FloatingState::PullNone
597 ),
598 (
599 gpio0.get_gpio_pin(13).unwrap(),
600 kernel::hil::gpio::ActivationMode::ActiveHigh,
601 kernel::hil::gpio::FloatingState::PullNone
602 ),
603 (
604 gpio0.get_gpio_pin(14).unwrap(),
605 kernel::hil::gpio::ActivationMode::ActiveHigh,
606 kernel::hil::gpio::FloatingState::PullNone
607 ),
608 (
609 gpio0.get_gpio_pin(15).unwrap(),
610 kernel::hil::gpio::ActivationMode::ActiveHigh,
611 kernel::hil::gpio::FloatingState::PullNone
612 ),
613 ),
614 )
615 .finalize(components::button_component_static!(GPIOPin));
616
617 let interrupt_service = static_init!(
620 LiteXSimInterruptablePeripherals,
621 LiteXSimInterruptablePeripherals {
622 gpio0,
623 uart0,
624 timer0,
625 ethmac0,
626 }
627 );
628 interrupt_service.init();
629
630 let chip = static_init!(
631 litex_vexriscv::chip::LiteXVexRiscv<
632 LiteXSimInterruptablePeripherals,
633 >,
634 litex_vexriscv::chip::LiteXVexRiscv::new(
635 "Verilated LiteX on VexRiscv",
636 interrupt_service,
637 pmp,
638 )
639 );
640
641 PANIC_REFERENCES.chip = Some(chip);
642
643 let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
644 .finalize(components::process_printer_text_component_static!());
645
646 PANIC_REFERENCES.process_printer = Some(process_printer);
647
648 csr::CSR
650 .mie
651 .modify(csr::mie::mie::mext::SET + csr::mie::mie::msoft::SET);
652 csr::CSR.mstatus.modify(csr::mstatus::mstatus::mie::SET);
653
654 chip.unmask_interrupts();
656
657 let console = components::console::ConsoleComponent::new(
659 board_kernel,
660 capsules_core::console::DRIVER_NUM,
661 uart_mux,
662 )
663 .finalize(components::console_component_static!());
664 components::debug_writer::DebugWriterComponent::new(
666 uart_mux,
667 create_capability!(capabilities::SetDebugWriterCapability),
668 )
669 .finalize(components::debug_writer_component_static!());
670
671 let lldb = components::lldb::LowLevelDebugComponent::new(
672 board_kernel,
673 capsules_core::low_level_debug::DRIVER_NUM,
674 uart_mux,
675 )
676 .finalize(components::low_level_debug_component_static!());
677
678 let scheduler = components::sched::mlfq::MLFQComponent::new(mux_alarm, processes).finalize(
679 components::mlfq_component_static!(
680 litex_vexriscv::timer::LiteXAlarm<
681 'static,
682 'static,
683 socc::SoCRegisterFmt,
684 socc::ClockFrequency,
685 >,
686 NUM_PROCS
687 ),
688 );
689
690 let litex_sim = LiteXSim {
691 gpio_driver,
692 button_driver,
693 led_driver,
694 console,
695 alarm,
696 lldb,
697 ipc: kernel::ipc::IPC::new(
698 board_kernel,
699 kernel::ipc::DRIVER_NUM,
700 &memory_allocation_cap,
701 ),
702 scheduler,
703 scheduler_timer,
704 };
705
706 debug!("Verilated LiteX+VexRiscv: initialization complete, entering main loop.");
707
708 kernel::process::load_processes(
709 board_kernel,
710 chip,
711 core::slice::from_raw_parts(
712 core::ptr::addr_of!(_sapps),
713 core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
714 ),
715 core::slice::from_raw_parts_mut(
716 core::ptr::addr_of_mut!(_sappmem),
717 core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
718 ),
719 &FAULT_RESPONSE,
720 &process_mgmt_cap,
721 )
722 .unwrap_or_else(|err| {
723 debug!("Error loading processes!");
724 debug!("{:?}", err);
725 });
726
727 (board_kernel, litex_sim, chip)
728}
729
730#[no_mangle]
732pub unsafe fn main() {
733 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
734
735 let (board_kernel, board, chip) = start();
736 board_kernel.kernel_loop(&board, chip, Some(&board.ipc), &main_loop_capability);
737}