xref: /aosp_15_r20/bootable/libbootloader/gbl/libboot/src/riscv64.rs (revision 5225e6b173e52d2efc6bcf950c27374fd72adabc)
1*5225e6b1SAndroid Build Coastguard Worker // Copyright 2023, The Android Open Source Project
2*5225e6b1SAndroid Build Coastguard Worker //
3*5225e6b1SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*5225e6b1SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*5225e6b1SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*5225e6b1SAndroid Build Coastguard Worker //
7*5225e6b1SAndroid Build Coastguard Worker //     http://www.apache.org/licenses/LICENSE-2.0
8*5225e6b1SAndroid Build Coastguard Worker //
9*5225e6b1SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*5225e6b1SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*5225e6b1SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*5225e6b1SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*5225e6b1SAndroid Build Coastguard Worker // limitations under the License.
14*5225e6b1SAndroid Build Coastguard Worker 
15*5225e6b1SAndroid Build Coastguard Worker //! Boot logic for RISC-V.
16*5225e6b1SAndroid Build Coastguard Worker 
17*5225e6b1SAndroid Build Coastguard Worker use core::arch::asm;
18*5225e6b1SAndroid Build Coastguard Worker 
19*5225e6b1SAndroid Build Coastguard Worker /// Boots a Linux kernel with the given boot hart ID and FDT blob.
20*5225e6b1SAndroid Build Coastguard Worker ///
21*5225e6b1SAndroid Build Coastguard Worker /// # Safety
22*5225e6b1SAndroid Build Coastguard Worker ///
23*5225e6b1SAndroid Build Coastguard Worker /// Caller must ensure that `kernel` contains a valid Linux kernel.
jump_linux(kernel: &[u8], boot_hart_id: usize, fdt: &[u8]) -> !24*5225e6b1SAndroid Build Coastguard Worker pub unsafe fn jump_linux(kernel: &[u8], boot_hart_id: usize, fdt: &[u8]) -> ! {
25*5225e6b1SAndroid Build Coastguard Worker     // No official documentation exists yet. This is equivalent to a C function call taking
26*5225e6b1SAndroid Build Coastguard Worker     // the hart ID and FDT address as parameters.
27*5225e6b1SAndroid Build Coastguard Worker     // SAFETY: By safety requirement of this function, `kernel` contains a valid linux kernel.
28*5225e6b1SAndroid Build Coastguard Worker     unsafe {
29*5225e6b1SAndroid Build Coastguard Worker         asm!(
30*5225e6b1SAndroid Build Coastguard Worker             "csrw satp, zero",
31*5225e6b1SAndroid Build Coastguard Worker             "jr {ep}",
32*5225e6b1SAndroid Build Coastguard Worker             in("a0") boot_hart_id,
33*5225e6b1SAndroid Build Coastguard Worker             in("a1") fdt.as_ptr() as usize,
34*5225e6b1SAndroid Build Coastguard Worker             ep = in(reg) kernel.as_ptr() as usize,
35*5225e6b1SAndroid Build Coastguard Worker         );
36*5225e6b1SAndroid Build Coastguard Worker     }
37*5225e6b1SAndroid Build Coastguard Worker 
38*5225e6b1SAndroid Build Coastguard Worker     unreachable!();
39*5225e6b1SAndroid Build Coastguard Worker }
40