0%

华为 hisi 1620

APEI

UEFI
ACPI Platform Error Interfaces

Provides a standard way to convey error info From Firmware to OS

BERT

Boot Error Record Table
Record fatal errors, then report it in the second boot
记录启动过程中的关键错误信息, 在下一次启动时报告错误.

  • 在 OS 未接管平台的控制权限之前 firmware(如 BIOS 或者 UEFI)检测到错误,导致系统无法继续启动,可以通过 BIOS/FIRMWARE 将这种类型的错误写入到特定的存储位置。这样一来,在下一次的正常启动过程中,OS 可以通过特定的方法将之前保存的错误读取出来分析并处理。
  • 系统运行过程中 firmware 检测到了致命错误,以至于 firmware 决定不通知 OS 而是直接重启(如 CPU 风扇突然坏了,瞬间过热,如果不立刻重启会烧毁 CPU),在重启前 firmware 可以记录下相关的错误信息以便之后分析出错原因

注:只有 BIOS/FIRMWARE 才有能力对 BERT 执行写入操作;对于 OS 而言,BERT 仅仅是一个只读的表。BERT 出现的意义在于希望采用一种统一的接口来记录特定类型的硬件错误(主要是一些致命的),从而简化 BIOS/FIRMWARE 和 OS 的实现。

阅读全文 »

RISC-V Better Atomics (Load-Acquire/Store-Release)

是 RISC-V 体系结构的一种扩展,旨在提供更强大的原子操作支持。
在并发编程中,原子操作是一种确保多个线程或处理器可以安全地对共享变量进行操作的机制。原子操作需要满足一定的一致性和同步性质,以避免数据竞争和不确定的行为。

RISC-V Better Atomics 扩展引入了一对新的原子指令,即 Load-Acquire 和 Store-Release。

  • Load-Acquire 用于读取共享变量,并确保读取操作具有”acquire”语义,即保证在此原子操作之前的所有存储操作都可见。
  • Store-Release 用于写入共享变量,并确保写入操作具有”release”语义,即保证在此原子操作之后的所有加载操作都能看到最新的值。

使用 RISC-V Better Atomics 可以更好地控制并发访问共享变量的顺序和一致性。它提供了更细粒度的原子操作语义,以满足高性能和并发编程的需求。

在 RISCV-V 的 A 扩展下

  • Load-Acquire 指令通常以 lr.amoswap 的形式出现,例如 lr.w(Load-Reserved Word)用于加载一个字(32位数据)并获取内存序。
  • Store-Release 指令通常以 sc.amoswap 的形式出现,例如 sc.w(Store-Conditional Word)用于将一个字(32位数据)存储到内存中并释放内存序。

Saturating Operations

饱和操作(Saturating Operations)是一种特殊的操作,用于处理数值溢出的情况。当执行某种算术操作时,如果结果超出了数据类型的表示范围,饱和操作将会将结果截断为数据类型所能表示的最大或最小值,而不是简单地截断为溢出的结果。

阅读全文 »

uefi 代码示例来自华为 hisi 1620

APEI

UEFI
ACPI Platform Error Interfaces

Provides a standard way to convey error info From Firmware to OS

BERT

Boot Error Record Table
Record fatal errors, then report it in the second boot
记录启动过程中的关键错误信息, 在下一次启动时报告错误.

  • 在 OS 未接管平台的控制权限之前 firmware(如 BIOS 或者 UEFI)检测到错误,导致系统无法继续启动,可以通过 BIOS/FIRMWARE 将这种类型的错误写入到特定的存储位置。这样一来,在下一次的正常启动过程中,OS 可以通过特定的方法将之前保存的错误读取出来分析并处理。
  • 系统运行过程中 firmware 检测到了致命错误,以至于 firmware 决定不通知 OS 而是直接重启(如 CPU 风扇突然坏了,瞬间过热,如果不立刻重启会烧毁 CPU),在重启前 firmware 可以记录下相关的错误信息以便之后分析出错原因

注:只有 BIOS/FIRMWARE 才有能力对 BERT 执行写入操作;对于 OS 而言,BERT 仅仅是一个只读的表。BERT 出现的意义在于希望采用一种统一的接口来记录特定类型的硬件错误(主要是一些致命的),从而简化 BIOS/FIRMWARE 和 OS 的实现。

阅读全文 »

  1. Crypto Acceleration: Not only for speed, but code protection
  2. True Random-Number Generators
  3. Memory Encryption: No leak from external bus!
  4. Secure Boot
  5. Trusted Execution Environment: RISC-V PMP IOPMP / Virtual Zone
  6. Tamper Pins Detection: Detect unauthorized opening or tampering
  7. Bus Monitors: Advanced hardware security
  8. hsm Hardware Secure Modules

secure boot

Chain of Trust:

  • bootrom (ZSBL)
  • spl (FSBL)
  • opensbi
  • u-boot
  • kernel
  • rootfs
  • system
    bootrom spl opensbi u-boot kernel rootfs 之间形成链式校验

    spl opensbi u-boot kernel rootfs 需要进行加密并加签, 在启动过程中需要解密并验签

system 采用dm-verity 校验.
之间可以引入hsm 进行算法加密和校验请求

The main services and features of the secure boot code are:
• Provides a Root of Trust for the SoC
• Initiates a Chain of Trust for the SoC
• Performs the First-Stage Boot Loader (FSBL) digital signature validation
• Supports Device Identifier Composition Engine (DICE)
• Guarantees Post-Quantum Cryptography (PQC) crypto-agility
• Proposes a secure exchange protocol for code/data programming, tests, and key provi-sioning services
• Proposes a secure boot code secure patch mechanism
• Considers domestic cryptographic requirements
• Complies with good practices and security standards
• Provides high flexibility in the proposed options above (enable/disable)

Hardware Mechanisms and Requirements
Requirements:
• The secure boot code MUST be stored in the SoC internal ROM
• The secure boot code MUST boot the FSBL in less than boot-time ms
• The size for the ROM containing the secure boot code MUST be at least 64KB
• The secure boot code MUST be ready for PQC crypto-agility
• The public keys MUST be stored in the OTP
• The OTP size MUST be at least 2KB (for ECDSA, more if RSA) for the secure boot code
• The OTP size MUST be at least 4KB for crypto-agility
• The OTP controller MUST be able to grant/revoke read-and-write access privileges
◦ For all or parts of the OTP
◦ The DICE UDS read access MUST be deactivable by the secure boot code
◦ The DICE UDS write access MUST be disabled
• The OTP read/write access privileges MUST be based on time condition
◦ For all or parts of the OTP
• The OTP read/write access privileges MAY be based on authentication conditions
◦ For all or parts of the OTP
• The secure boot code MUST support the ECDSA p384/SHA384 digital signature algo-
rithm
• The secure boot code MUST support the SM2/SM3 digital signature algorithm
• The secure boot code MUST support the FALCON digital signature algorithm
• The secure boot code MAY support the ECDSA p256/SHA256 digital signature algorithm
• The secure hash SHA-384 implementation MUST be a hardware block (for performance
requirements)
◦ This hardware block MUST be accessible from the core executing the secure boot
code
• The secure hash SM3 implementation MUST be a hardware block (for performance
requirements)
◦ This hardware block MUST be accessible from the core executing the secure boot
code
• The ECDSA and SM2 algorithms MUST have hardware support for performance (e.g.,
modular multiplication)
◦ This hardware block MUST be accessible from the core executing the secure boot
code
• A communication interface MUST be available to the secure boot code, usually a UART
◦ This interface is used for initial and recovery programming
• A GPIO MUST be available for triggering the secure communication session
• The internal RAM size MUST be at least 64KB
◦ It depends on the size of the applets (a feature used in the communication protocol to
extend commands; it’s very helpful to simplify the secure boot code)
• The NVM where the FSBL is located MUST be available to the secure boot code in read
(for execution) and write (for programming) modes
• The debug availability policy MUST be specified
◦ If available, the secure boot code MAY be in charge of enabling it
• The secure boot code MUST contain anti-fault mechanisms during sensitive computations
◦ Note: The other subsequent parts of the secure boot process, too
• The secure boot code software validation MUST assess the robustness against faults
(and not be only a functional validation)
• The [rng] (for secure provisioning service) MUST be accessible by the secure boot code
• A [hash-function] hardware block and a managing FSM MUST be available for secure
boot code verification before start
• The secure boot code MUST be in charge of the DICE CDI computation for the FSBL
using the UDS from the OTP
◦ The UDS read access MUST be disabled after CDI computation
◦ The debug interface MUST NOT be available before UDS read access disabling
• The secure boot code MUST be written using good practices for secure coding, including
those presented in: https://www.ssi.gouv.fr/uploads/2020/05/anssi-guide-regles_de_pro-
grammation_pour_le_developpement_securise_de_logiciels_en_langage_c-v1.1.pdf
• The secure boot code MUST clean any sensitive information contained in the volatile
memory after its use
• The secure boot code MUST support GPT parsing for FSBL image booting
• The secure boot code MUST implement an anti-rollback protection mechanism
◦ The anti-rollback mechanism MUST support at least 32 versions
▪ It requires 128 bytes for version storage
◦ The anti-rollback mechanism MUST use the internal OTP

DCIE
DICE是一个用于设备身份、证明和数据加密的行业标准。它要求使用一些加密原语(哈希、HMAC)和每个SoC唯一的256位秘密,命名为UDS。这个UDS必须得到严格的保护,以防止嵌入式软件的任何读取访问。其原理是将一个独特的、可重复的秘钥与设备上运行的每个软件阶段层联系起来。最初的秘钥,也就是与FSBL绑定的秘钥,又称第0层,是由安全启动码使用FSBL的哈希值计算出来的。FSBL可以通过计算下一个CDI,对应于下一个软件阶段(如U-boot)来重现相同的方案,以此类推,直到达到最后一个应用阶段。然后,任何一层都有自己的秘钥(公钥对也可以从CDI中得到)。

Key Management

阅读全文 »

概述

virtio-scsi 提供了直接连接 SCSI LUN 的能力,并且也提供了继承目标设备特性的能力:
通过 virtio-scsi 控制器连接的虚拟硬盘或 CD,可以从 host 主机通过 QEMU scsi-block 设备实现物理 SCSI 设备的直通 (pass-through),这样就可以实现每个 guest 使用上百个设备,也提供了极高的存储性能。

guest 与 host 链路:
guest: app -> 文件系统-> Block Layer -> SCSI Layer -> scsi_mod
host: SCSI Layer -> Block Device Driver -> Hardware

前端 后端
virtio-scsi—qemu virtio-scsi
virtio-scsi—kernel vhost-scsi

直通方式

kernel vhost-scsi

QEMU负责对该PCI设备的模拟,只是把来自virtqueue的数据处理逻辑拿到内核空间了。QEMU需要告知内核vhost-scsi模块关于virtqueue的内存信息及Guest的内存映射,这样其实省去了Guest到QEMU用户态空间,再到宿主机内核空间多次数据复制; 但是,将queue的ID写到PCI配置空间的这步操作还是存在。

image-20240416111040666

通过vfio-pci直接进行SATA控制器直通

所有PC上的SATA控制器都在PCI总线上运行。您可以插入备用SATA控制器并通过它。所有连接的磁盘将直接传递给客户机。该解决方案几乎没有延迟或开销,并提供了最高的吞吐量。该解决方案的缺点是您将需要专门用于VM的第二个SATA控制器。来宾运行时,主机上的磁盘也将不可用。

阅读全文 »

多分区镜像如何挂载

利用mount -o offset选项进行挂载。即偏移地址。

1
sudo mount -v -o offset=1048576 -t vfat sdimage.img ~/mount/boot/

Units: sectors of 1 * 512 = 512 bytes
一个扇区为512bytes。

1
2
3
4
5
sudo fdisk -l starfive-jh7110-VF2_515_v2.5.0-69-minimal-desktop.img
Device Start End Sectors Size Type
starfive-jh7110-VF2_515_v2.5.0-69-minimal-desktop.img1 2048 34815 32768 16M Linux filesystem
starfive-jh7110-VF2_515_v2.5.0-69-minimal-desktop.img2 34816 239615 204800 100M EFI System
starfive-jh7110-VF2_515_v2.5.0-69-minimal-desktop.img3 239616 3479518 3239903 1.6G Linux filesystem

如挂载第三个分区
offset = 239616 * 512

1
sudo mount -o offset=122683392 starfive-jh7110-VF2_515_v2.5.0-69-minimal-desktop.img test_mnt_dir 

qcow2 格式

制作

1
qemu-img create -f qcow2 image.qcow2 1G
阅读全文 »

USB控制器类型

简单地讲,OHCI、UHCI都是USB1.1的接口标准,而EHCI是对应USB2.0的接口标准最新的xHCI是USB3.0的接口标准

  • OHCI(Open Host Controller Interface)是支持USB1.1的标准,但它不仅仅是针对USB,还支持其他的一些接口,比如它还支持Apple的火线(Firewire,IEEE 1394)接口。与UHCI相比,OHCI的硬件复杂,硬件做的事情更多,所以实现对应的软件驱动的任务,就相对较简单。主要用于非x86的USB,如扩展卡、嵌入式开发板的USB主控。
  • UHCI(Universal Host Controller Interface),是Intel主导的对USB1.0、1.1的接口标准,与OHCI不兼容。UHCI的软件驱动的任务重,需要做得比较复杂,但可以使用较便宜、较简单的硬件的USB控制器。Intel和VIA使用UHCI,而其余的硬件提供商使用OHCI。
  • EHCI(Enhanced Host Controller Interface),是Intel主导的USB2.0的接口标准。EHCI仅提供USB2.0的高速功能,而依靠UHCI或OHCI来提供对全速(full-speed)或低速(low-speed)设备的支持。
  • xHCI(eXtensible Host Controller Interface),是最新最火的USB3.0的接口标准,它在速度、节能、虚拟化等方面都比前面3中有了较大的提高。xHCI支持所有种类速度的USB设备(USB 3.0 SuperSpeed, USB 2.0 Low-, Full-, and High-speed, USB 1.1 Low- and Full-speed)。xHCI的目的是为了替换前面3中(UHCI/OHCI/EHCI)。

usb 层级

用lsusb -t还可以看到USB设备的层级关系

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@localhost xqk]# lsusb -t
/: Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
|__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/8p, 480M
|__ Port 1: Dev 3, If 0, Class=Mass Storage, Driver=usb-storage, 480M


Bus 02.Port 1
表示第二个USB主控制器,Port号为1

Dev 1, Class=root_hub
分配的设备号为1,类型是root_hub

Driver=ehci-pci/2p
root_hub的类型是ehci(usb 2.0),总共有两个port

|__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/8p, 480M
root_hub的其中一个port有个Hub设备,port的id是1,此Hub有8个port。

|__ Port 1: Dev 3, If 0, Class=Mass Storage, Driver=usb-storage, 480M
Hub的其中一个port有大容量USB设备,port的id为1.3(树状结构,依次以.作为低一级设备成员)

USB设备直通

虚拟机使用USB设备可以通过两种方式:

  • PCI 设备直通

    PCI usb card, 直接将 usb controller 直通给虚拟机

  • 设备直通

这里重点说下非PCI方式的usb 设备直通
qemu支持hostaddr,hostport以及vendorid,productid等信息的组合, 相对于libvirt,多了hostport的支持,hostport即是主机usb的port。

阅读全文 »

虚拟化技术概述

虚拟机特性:

  • 指令模拟 (guest 和 host 可以是不同的isa)
  • 本地指令直接执行( guest 和 host isa是相同的)

系统资源虚拟化:

  • cpu虚拟化
  • 内存虚拟化
  • io 虚拟化

常见的虚拟机软件:

  • vmware
  • virtual box
  • Parallels Desktop
  • Xen
  • linux-kvm qemu
  • Xvisor

image-20240416111454675

Hypervisor Virtual Machine Manager(VMM) 的功能

  • 控制所有的系统资源(CPU 内存 网络 存储等)
  • 创建虚拟机并分配响应的资源
  • 管理虚拟机的生命周期
阅读全文 »

背景

  • Read the fucking source code! –By 鲁迅
  • A picture is worth a thousand words. –By 高尔基

说明:

  1. Kernel 版本:4.14
  2. ARM64 处理器
  3. 使用工具:Source Insight 3.5, Visio

1. 概述

从本文开始,将会针对 PCIe 专题来展开,涉及的内容包括:

  1. PCI/PCIe 总线硬件;
  2. Linux PCI 驱动核心框架;
  3. Linux PCI Host 控制器驱动;

不排除会包含 PCIe 外设驱动模块,一切随缘。

作为专题的第一篇,当然会先从硬件总线入手。
进入主题前,先讲点背景知识。
在 PC 时代,随着处理器的发展,经历了几代 I/O 总线的发展,解决的问题都是 CPU 主频提升与外部设备访问速度的问题:

  1. 第一代总线包含ISAEISAVESAMicro Channel等;
  2. 第二代总线包含PCIAGPPCI-X等;
  3. 第三代总线包含PCIemPCIem.2等;
阅读全文 »

背 景

  • Read the fucking source code! –By 鲁迅
  • A picture is worth a thousand words. –By 高尔基

说明:

  1. Kernel 版本:4.14
  2. ARM64 处理器
  3. 使用工具:Source Insight 3.5, Visio

1. 概述

  • 本文将分析 Linux PCI 子系统的框架,主要围绕 Linux PCI 子系统的初始化以及枚举过程分析;
  • 如果对具体的硬件缺乏了解,建议先阅读上篇文章《Linux PCI驱动框架分析(一)》

话不多说,直接开始。

2. 数据结构

image-20240416110922011

  • PCI 体系结构的拓扑关系如图所示,而图中的不同数据结构就是用于来描述对应的模块;
  • Host Bridge 连接 CPU 和 PCI 系统,由struct pci_host_bridge描述;
  • struct pci_dev描述 PCI 设备,以及 PCI-to-PCI 桥设备;
  • struct pci_bus用于描述 PCI 总线,struct pci_slot用于描述总线上的物理插槽;
阅读全文 »