UARTドライバーを書いてみましょう

QEMU の virt マシンには PL011 UART があるので、そのドライバーを書いてみましょう。

// 著作権 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0

const FLAG_REGISTER_OFFSET: usize = 0x18;
const FR_BUSY: u8 = 1 << 3;
const FR_TXFF: u8 = 1 << 5;

/// Minimal driver for a PL011 UART.
#[derive(Debug)]
pub struct Uart {
    base_address: *mut u8,
}

impl Uart {
    /// Constructs a new instance of the UART driver for a PL011 device at the
    /// given base address.
    ///
    /// # Safety
    ///
    /// The given base address must point to the 8 MMIO control registers of a
    /// PL011 device, which must be mapped into the address space of the process
    /// as device memory and not have any other aliases.
    pub unsafe fn new(base_address: *mut u8) -> Self {
        Self { base_address }
    }

    /// Writes a single byte to the UART.
    pub fn write_byte(&self, byte: u8) {
        // Wait until there is room in the TX buffer.
        while self.read_flag_register() & FR_TXFF != 0 {}

        // SAFETY: We know that the base address points to the control
        // registers of a PL011 device which is appropriately mapped.
        unsafe {
            // Write to the TX buffer.
            self.base_address.write_volatile(byte);
        }

        // Wait until the UART is no longer busy.
        while self.read_flag_register() & FR_BUSY != 0 {}
    }

    fn read_flag_register(&self) -> u8 {
        // SAFETY: We know that the base address points to the control
        // registers of a PL011 device which is appropriately mapped.
        unsafe { self.base_address.add(FLAG_REGISTER_OFFSET).read_volatile() }
    }
}
  • Uart::new は unsafe ですが、他のメソッドは safe であることに注意してください。これは、Uart::new の呼び出し側がその安全性要件(つまり、特定の UART に対してこのドライバーのインスタンスが常に 1 つしか存在せず、そのアドレス空間をエイリアスするものが他に存在しないこと)を満たすことを保証している限り、必要な事前条件が成り立つと仮定できるため、後で write_byte を呼び出すことは常に安全だからです。
  • 逆にすることもできました(new を safe にして write_byte を unsafe にする)が、そうすると write_byte を呼び出すすべての箇所で安全性について考えなければならず、使い勝手は大幅に悪くなります
  • これは、unsafe なコードに対する safe なラッパーを書く際によくあるパターンです。つまり、健全性の立証責任を多数の箇所から、より少ない箇所へ移すということです。