fail.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. use crate::riscv::current_hartid;
  2. use serde_device_tree::Dtb;
  3. use crate::devicetree;
  4. #[cfg(all(feature = "payload", feature = "jump"))]
  5. compile_error!("feature \"payload\" and feature \"jump\" cannot be enabled at the same time");
  6. #[panic_handler]
  7. fn panic(info: &core::panic::PanicInfo) -> ! {
  8. use ::riscv::register::*;
  9. error!("Hart {} {info}", current_hartid());
  10. error!("-----------------------------");
  11. error!("mcause: {:?}", mcause::read().cause());
  12. error!("mepc: {:#018x}", mepc::read());
  13. error!("mtval: {:#018x}", mtval::read());
  14. error!("-----------------------------");
  15. error!("System shutdown scheduled due to RustSBI panic");
  16. loop {}
  17. }
  18. /// Handles device tree format parsing errors by logging and resetting.
  19. #[cold]
  20. pub fn device_tree_format(_err: devicetree::ParseDeviceTreeError) -> Dtb {
  21. loop {
  22. core::hint::spin_loop()
  23. }
  24. }
  25. #[cold]
  26. pub fn device_tree_deserialize_root<'a>(
  27. _err: serde_device_tree::error::Error,
  28. ) -> serde_device_tree::buildin::Node<'a> {
  29. loop {
  30. core::hint::spin_loop()
  31. }
  32. }
  33. cfg_if::cfg_if! {
  34. if #[cfg(feature = "payload")] {
  35. } else if #[cfg(feature = "jump")] {
  36. } else {
  37. use crate::firmware::dynamic;
  38. use crate::sbi::reset;
  39. use riscv::register::mstatus;
  40. /// Handles invalid dynamic information data by logging details and resetting.
  41. #[cold]
  42. pub fn invalid_dynamic_data(err: dynamic::DynamicError) -> (mstatus::MPP, usize) {
  43. error!("Invalid data in dynamic information:");
  44. if err.invalid_mpp {
  45. error!("* dynamic information contains invalid privilege mode");
  46. }
  47. if err.invalid_next_addr {
  48. error!("* dynamic information contains invalid next jump address");
  49. }
  50. let explain_next_mode = match err.bad_info.next_mode {
  51. 3 => "Machine",
  52. 1 => "Supervisor",
  53. 0 => "User",
  54. _ => "Invalid",
  55. };
  56. error!(
  57. "@ help: dynamic information contains magic value 0x{:x}, version {}, next jump address 0x{:x}, next privilege mode {} ({}), options {:x}, boot hart ID {}",
  58. err.bad_info.magic, err.bad_info.version, err.bad_info.next_addr, err.bad_info.next_mode, explain_next_mode, err.bad_info.options, err.bad_info.boot_hart
  59. );
  60. reset::fail()
  61. }
  62. /// Handles case where dynamic information is not available by logging details and resetting.
  63. #[cold]
  64. pub fn no_dynamic_info_available(err: dynamic::DynamicReadError) -> dynamic::DynamicInfo {
  65. if let Some(bad_paddr) = err.bad_paddr {
  66. error!(
  67. "No dynamic information available at address 0x{:x}",
  68. bad_paddr
  69. );
  70. } else {
  71. error!("No valid dynamic information available:");
  72. if let Some(bad_magic) = err.bad_magic {
  73. error!(
  74. "* tried to identify dynamic information, but found invalid magic number 0x{:x}",
  75. bad_magic
  76. );
  77. }
  78. if let Some(bad_version) = err.bad_version {
  79. error!("* tries to identify version of dynamic information, but the version number {} is not supported", bad_version);
  80. }
  81. if err.bad_magic.is_none() {
  82. error!("@ help: magic number is valid")
  83. }
  84. if err.bad_version.is_none() {
  85. error!("@ help: dynamic information version is valid")
  86. }
  87. }
  88. reset::fail()
  89. }
  90. /// Fallback function that returns default dynamic info with boot_hart set to MAX.
  91. ///
  92. /// Used when dynamic info read fails but execution should continue.
  93. #[cold]
  94. pub fn use_lottery(_err: dynamic::DynamicReadError) -> dynamic::DynamicInfo {
  95. dynamic::DynamicInfo {
  96. magic: 0,
  97. version: 0,
  98. next_addr: 0,
  99. next_mode: 0,
  100. options: 0,
  101. boot_hart: usize::MAX,
  102. }
  103. }
  104. }
  105. }