| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 |
|
| 9 | use crate::{
|
| 10 | ffi::{c_char, c_void},
|
| 11 | fmt,
|
| 12 | prelude::*,
|
| 13 | str::RawFormatter,
|
| 14 | sync::atomic::{
|
| 15 | Atomic,
|
| 16 | AtomicType,
|
| 17 | Relaxed, |
| 18 | },
|
| 19 | };
|
| 20 |
|
| 21 | |
| 22 | #[expect(clippy::missing_safety_doc)]
|
| 23 | #[export]
|
| 24 | unsafe extern "C" fn rust_fmt_argument(
|
| 25 | buf: *mut c_char,
|
| 26 | end: *mut c_char,
|
| 27 | ptr: *const c_void,
|
| 28 | ) -> *mut c_char {
|
| 29 | use fmt::Write;
|
| 30 | |
| 31 | let mut w = unsafe { RawFormatter::from_ptrs(buf.cast(), end.cast()) };
|
| 32 | |
| 33 | let _ = w.write_fmt(unsafe { *ptr.cast::<fmt::Arguments<'_>>() });
|
| 34 | w.pos().cast()
|
| 35 | }
|
| 36 |
|
| 37 | |
| 38 | |
| 39 | |
| 40 | #[doc(hidden)]
|
| 41 | pub mod format_strings {
|
| 42 | |
| 43 | const LENGTH_PREFIX: usize = 2;
|
| 44 |
|
| 45 | |
| 46 | pub const LENGTH: usize = 10;
|
| 47 |
|
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | const fn generate(is_cont: bool, prefix: &[u8; 3]) -> [u8; LENGTH] {
|
| 55 | |
| 56 | assert!(prefix[0] == b'\x01');
|
| 57 | if is_cont {
|
| 58 | assert!(prefix[1] == b'c');
|
| 59 | } else {
|
| 60 | assert!(prefix[1] >= b'0' && prefix[1] <= b'7');
|
| 61 | }
|
| 62 | assert!(prefix[2] == b'\x00');
|
| 63 |
|
| 64 | let suffix: &[u8; LENGTH - LENGTH_PREFIX] = if is_cont {
|
| 65 | b"%pA\0\0\0\0\0"
|
| 66 | } else {
|
| 67 | b"%s: %pA\0"
|
| 68 | };
|
| 69 |
|
| 70 | [
|
| 71 | prefix[0], prefix[1], suffix[0], suffix[1], suffix[2], suffix[3], suffix[4], suffix[5],
|
| 72 | suffix[6], suffix[7],
|
| 73 | ]
|
| 74 | }
|
| 75 |
|
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | pub static EMERG: [u8; LENGTH] = generate(false, bindings::KERN_EMERG);
|
| 83 | pub static ALERT: [u8; LENGTH] = generate(false, bindings::KERN_ALERT);
|
| 84 | pub static CRIT: [u8; LENGTH] = generate(false, bindings::KERN_CRIT);
|
| 85 | pub static ERR: [u8; LENGTH] = generate(false, bindings::KERN_ERR);
|
| 86 | pub static WARNING: [u8; LENGTH] = generate(false, bindings::KERN_WARNING);
|
| 87 | pub static NOTICE: [u8; LENGTH] = generate(false, bindings::KERN_NOTICE);
|
| 88 | pub static INFO: [u8; LENGTH] = generate(false, bindings::KERN_INFO);
|
| 89 | pub static DEBUG: [u8; LENGTH] = generate(false, bindings::KERN_DEBUG);
|
| 90 | pub static CONT: [u8; LENGTH] = generate(true, bindings::KERN_CONT);
|
| 91 | }
|
| 92 |
|
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | #[doc(hidden)]
|
| 104 | #[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
|
| 105 | pub unsafe fn call_printk(
|
| 106 | format_string: &[u8; format_strings::LENGTH],
|
| 107 | module_name: &[u8],
|
| 108 | args: fmt::Arguments<'_>,
|
| 109 | ) {
|
| 110 | |
| 111 | #[cfg(CONFIG_PRINTK)]
|
| 112 | |
| 113 | unsafe {
|
| 114 | bindings::_printk(
|
| 115 | format_string.as_ptr(),
|
| 116 | module_name.as_ptr(),
|
| 117 | core::ptr::from_ref(&args).cast::<c_void>(),
|
| 118 | );
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | #[doc(hidden)]
|
| 128 | #[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
|
| 129 | pub fn call_printk_cont(args: fmt::Arguments<'_>) {
|
| 130 | |
| 131 | |
| 132 | |
| 133 | #[cfg(CONFIG_PRINTK)]
|
| 134 | unsafe {
|
| 135 | bindings::_printk(
|
| 136 | format_strings::CONT.as_ptr(),
|
| 137 | core::ptr::from_ref(&args).cast::<c_void>(),
|
| 138 | );
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | |
| 143 | |
| 144 | |
| 145 | #[doc(hidden)]
|
| 146 | #[cfg(not(testlib))]
|
| 147 | #[macro_export]
|
| 148 | #[expect(clippy::crate_in_macro_def)]
|
| 149 | macro_rules! print_macro (
|
| 150 | |
| 151 | ($format_string:path, false, $($arg:tt)+) => (
|
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | match $crate::prelude::fmt!($($arg)+) {
|
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | args => unsafe {
|
| 164 | $crate::print::call_printk(
|
| 165 | &$format_string,
|
| 166 | crate::__LOG_PREFIX,
|
| 167 | args,
|
| 168 | );
|
| 169 | }
|
| 170 | }
|
| 171 | );
|
| 172 |
|
| 173 | |
| 174 | ($format_string:path, true, $($arg:tt)+) => (
|
| 175 | $crate::print::call_printk_cont(
|
| 176 | $crate::prelude::fmt!($($arg)+),
|
| 177 | );
|
| 178 | );
|
| 179 | );
|
| 180 |
|
| 181 | |
| 182 | #[cfg(testlib)]
|
| 183 | #[macro_export]
|
| 184 | macro_rules! print_macro (
|
| 185 | ($format_string:path, $e:expr, $($arg:tt)+) => (
|
| 186 | ()
|
| 187 | );
|
| 188 | );
|
| 189 |
|
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 |
|
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | #[macro_export]
|
| 218 | macro_rules! pr_emerg (
|
| 219 | ($($arg:tt)*) => (
|
| 220 | $crate::print_macro!($crate::print::format_strings::EMERG, false, $($arg)*)
|
| 221 | )
|
| 222 | );
|
| 223 |
|
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | #[macro_export]
|
| 243 | macro_rules! pr_alert (
|
| 244 | ($($arg:tt)*) => (
|
| 245 | $crate::print_macro!($crate::print::format_strings::ALERT, false, $($arg)*)
|
| 246 | )
|
| 247 | );
|
| 248 |
|
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | #[macro_export]
|
| 268 | macro_rules! pr_crit (
|
| 269 | ($($arg:tt)*) => (
|
| 270 | $crate::print_macro!($crate::print::format_strings::CRIT, false, $($arg)*)
|
| 271 | )
|
| 272 | );
|
| 273 |
|
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | #[macro_export]
|
| 293 | macro_rules! pr_err (
|
| 294 | ($($arg:tt)*) => (
|
| 295 | $crate::print_macro!($crate::print::format_strings::ERR, false, $($arg)*)
|
| 296 | )
|
| 297 | );
|
| 298 |
|
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | #[macro_export]
|
| 318 | macro_rules! pr_warn (
|
| 319 | ($($arg:tt)*) => (
|
| 320 | $crate::print_macro!($crate::print::format_strings::WARNING, false, $($arg)*)
|
| 321 | )
|
| 322 | );
|
| 323 |
|
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | #[macro_export]
|
| 343 | macro_rules! pr_notice (
|
| 344 | ($($arg:tt)*) => (
|
| 345 | $crate::print_macro!($crate::print::format_strings::NOTICE, false, $($arg)*)
|
| 346 | )
|
| 347 | );
|
| 348 |
|
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | #[macro_export]
|
| 368 | #[doc(alias = "print")]
|
| 369 | macro_rules! pr_info (
|
| 370 | ($($arg:tt)*) => (
|
| 371 | $crate::print_macro!($crate::print::format_strings::INFO, false, $($arg)*)
|
| 372 | )
|
| 373 | );
|
| 374 |
|
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | #[macro_export]
|
| 395 | #[doc(alias = "print")]
|
| 396 | macro_rules! pr_debug (
|
| 397 | ($($arg:tt)*) => (
|
| 398 | if cfg!(debug_assertions) {
|
| 399 | $crate::print_macro!($crate::print::format_strings::DEBUG, false, $($arg)*)
|
| 400 | }
|
| 401 | )
|
| 402 | );
|
| 403 |
|
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | #[macro_export]
|
| 426 | macro_rules! pr_cont (
|
| 427 | ($($arg:tt)*) => (
|
| 428 | $crate::print_macro!($crate::print::format_strings::CONT, true, $($arg)*)
|
| 429 | )
|
| 430 | );
|
| 431 |
|
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | pub struct OnceLite(Atomic<State>);
|
| 439 |
|
| 440 | #[derive(Clone, Copy, PartialEq, Eq)]
|
| 441 | #[repr(i32)]
|
| 442 | enum State {
|
| 443 | Incomplete = 0,
|
| 444 | Complete = 1,
|
| 445 | }
|
| 446 |
|
| 447 | |
| 448 | |
| 449 | unsafe impl AtomicType for State {
|
| 450 | type Repr = i32;
|
| 451 | }
|
| 452 |
|
| 453 | impl OnceLite {
|
| 454 | |
| 455 | #[inline(always)]
|
| 456 | #[allow(clippy::new_without_default)]
|
| 457 | pub const fn new() -> Self {
|
| 458 | OnceLite(Atomic::new(State::Incomplete))
|
| 459 | }
|
| 460 |
|
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | pub fn call_once<F>(&self, f: F) -> bool
|
| 469 | where
|
| 470 | F: FnOnce(),
|
| 471 | {
|
| 472 | |
| 473 | |
| 474 | let old = self.0.load(Relaxed);
|
| 475 | if old == State::Complete {
|
| 476 | return false;
|
| 477 | }
|
| 478 |
|
| 479 | |
| 480 | let old = self.0.xchg(State::Complete, Relaxed);
|
| 481 | if old == State::Complete {
|
| 482 | return false;
|
| 483 | }
|
| 484 |
|
| 485 | f();
|
| 486 | true
|
| 487 | }
|
| 488 | }
|
| 489 |
|
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | #[macro_export]
|
| 502 | macro_rules! do_once_lite {
|
| 503 | { $($e:tt)* } => {{
|
| 504 | #[link_section = ".data..once"]
|
| 505 | static ONCE: $crate::print::OnceLite = $crate::print::OnceLite::new();
|
| 506 | ONCE.call_once(|| { $($e)* });
|
| 507 | }};
|
| 508 | }
|
| 509 |
|
| 510 | |
| 511 | |
| 512 | |
| 513 | #[macro_export]
|
| 514 | macro_rules! pr_emerg_once (
|
| 515 | ($($arg:tt)*) => (
|
| 516 | $crate::do_once_lite! { $crate::pr_emerg!($($arg)*) }
|
| 517 | )
|
| 518 | );
|
| 519 |
|
| 520 | |
| 521 | |
| 522 | |
| 523 | #[macro_export]
|
| 524 | macro_rules! pr_alert_once (
|
| 525 | ($($arg:tt)*) => (
|
| 526 | $crate::do_once_lite! { $crate::pr_alert!($($arg)*) }
|
| 527 | )
|
| 528 | );
|
| 529 |
|
| 530 | |
| 531 | |
| 532 | |
| 533 | #[macro_export]
|
| 534 | macro_rules! pr_crit_once (
|
| 535 | ($($arg:tt)*) => (
|
| 536 | $crate::do_once_lite! { $crate::pr_crit!($($arg)*) }
|
| 537 | )
|
| 538 | );
|
| 539 |
|
| 540 | |
| 541 | |
| 542 | |
| 543 | #[macro_export]
|
| 544 | macro_rules! pr_err_once (
|
| 545 | ($($arg:tt)*) => (
|
| 546 | $crate::do_once_lite! { $crate::pr_err!($($arg)*) }
|
| 547 | )
|
| 548 | );
|
| 549 |
|
| 550 | |
| 551 | |
| 552 | |
| 553 | #[macro_export]
|
| 554 | macro_rules! pr_warn_once (
|
| 555 | ($($arg:tt)*) => (
|
| 556 | $crate::do_once_lite! { $crate::pr_warn!($($arg)*) }
|
| 557 | )
|
| 558 | );
|
| 559 |
|
| 560 | |
| 561 | |
| 562 | |
| 563 | #[macro_export]
|
| 564 | macro_rules! pr_notice_once (
|
| 565 | ($($arg:tt)*) => (
|
| 566 | $crate::do_once_lite! { $crate::pr_notice!($($arg)*) }
|
| 567 | )
|
| 568 | );
|
| 569 |
|
| 570 | |
| 571 | |
| 572 | |
| 573 | #[macro_export]
|
| 574 | macro_rules! pr_info_once (
|
| 575 | ($($arg:tt)*) => (
|
| 576 | $crate::do_once_lite! { $crate::pr_info!($($arg)*) }
|
| 577 | )
|
| 578 | );
|