ARM v8 简介

article/2025/11/10 6:18:38

ARMv8

Exception Level

在介绍其他概念前,先要了解 ARMv8 全新的异常级设计。
ARMv8 定义了4个异常级。EL0-EL3,数字越大,权限越高。其中 EL0 用于应用程序,EL1 用于操作系统,EL2 用于虚拟化,EL3 用于安全固件。对于处理器实现而言,EL0和EL1是必须要实现的,EL2和EL3可选,且不必连续,例如可以实现 EL0,EL1和EL3,不实现EL2。

Execution State

ARMv8为了兼容32位指令有两个执行态,分别是 AArch32 和 AArch64,AArch32 兼容 Armv7-A。
各个异常级支持的执行态见下图:可以看到64位的OS可以支持32位和64位的APP,32位的OS只能支持32的APP。
道理很简单,如果64的APP运行在32位的OS上,syscall 的参数将被OS截断。同理 EL2 的 Hypervisor 也是如此。
在这里插入图片描述
执行态的切换必须要进入更高的EL。例如,EL0当前运行在AArch32希望切换到AArch64,必须向OS请求来实现执行态的变更。这里没有提到EL3,如果EL3希望切换执行态该怎么办呢?由于EL3是最高的异常级了,所以自己没办法改变执行态,只能通过复位配置来解决。对于其他异常级而言,可以通过System Register来控制。

If, when taking an exception or returning from an exception, the Exception level remains the same, the execution state cannot change.

PSTATE

程序状态字。包含各种标志、进位、溢出等。

In the ARMv8-A architecture, Process state or PSTATE is an abstraction of process state information.

ARMv8 Registers

体系结构提供了两类寄存器,一种用于系统控制和状态呈现,称为系统寄存器。一种用于指令处理,比如算术运算或异常处理。接下来描述一下后者。

  • 通用寄存器 R0-R30
  • 栈指针寄存器 SP_ELx
  • SIMD及浮点寄存器
  • 保存程序状态寄存器 SPSR
  • 异常链接寄存器 ELRx

General Purpose Registers

ARMv8 提供了31个 64-bit 的通用寄存器。分别是 X0-X30。
每个 64-bit 的寄存器低32位又包含 32-bit 的兼容形式。称作 W0-W30。
读取W不会影响相应X的高32位,写入W会清零相应X的高32位。

Special Registers

在这里插入图片描述
In the ARMv8 architecture, when executing in AArch64, the exception return state is held in the
following dedicated registers for each Exception level:

  • Exception Link Register (ELR).
  • Saved Processor State Register (SPSR).

There is a dedicated SP per Exception level, but it is not used to hold return state.

Zero register

这个寄存器读取值时返回0,写入值时值被忽略。

Stack pointer

在每个实现的 Exception level 上都有相应的SP寄存器。
EL0 只能使用 SP_EL0 作为栈指针。
进入其他异常级后可以选择当前级的 SP_ELx 或者选择 SP_EL0 作为堆栈指针。触发异常时默认选择目标异常级的栈指针,当触发异常时,即使异常级未发生变化,也会切到目标异常级对应的栈指针上,也就是说假如当前在 EL1 使用 SP_EL0,此时触发了异常,依然停留在 EL1,栈指针将切换到 SP_EL1,而不是之前设置的 SP_EL0。

Software executing at the target Exception level can then choose to change the stack pointer to SP_EL0 by updating PSTATE.SP.

我们可以通过在异常级的后缀区分到底用了哪个栈指针。

// The t and h suffixes are based on the terminology of thread and handler.
t Indicates use of the SP_EL0 stack pointer.
h Indicates use of the SP_ELx stack pointer.

SIMD & FP

单指令多数据和浮点指令共享一套寄存器组。
它们是128-bit 的寄存器组,V0-V31。同时可以以不同的长度访问这些寄存器,分别是:

  • 64-bit D0-D31
  • 32-bit S0-S31
  • 16-bit H0-H31
  • 8-bit B0-B31

Saved Process Status Register

保存异常发生时的PSTATE值。SPSR在各个异常级同样都有对应的 SPSR_ELx。

Exception Link Register

保存异常发生时的返回地址。ELR在各个异常级同样有对应的 ELR_ELx,异常返回时 PC 的值讲从 ELR 中恢复。异常发生时 ELR_ELx 中保存的值和异常类型息息相关,见下文的 Exception return

事实上操作系统就是通过 ELR_EL1 的值来控制返回 EL0 时的指令地址,实现跳转。

System Registers

这些寄存器负责系统配置。且使用MSR, MRS指令进行控制。通常 EL0 是无法访问这些寄存器的。
The name of a register tells you the lowest Exception level that it can be accessed from.
For example:

  • TTBR0_EL1 is accessible from EL1, EL2, and EL3.
  • TTBR0_EL2 is accessible from EL2 and EL3.
MRS x0, TTBR0_EL1 // Move TTBR0_EL1 into x0
MSR TTBR0_EL1, x0 // Move x0 into TTBR0_EL1

Exception

Synchronous & Asynchronous exception

异常是指系统发生了需要特权级软件(exception handler)进行处理的状态或事件。它会打断当前指令流,强制跳转到预先定义的位置进行处理。ARMv8 将异常分为了两个大类,分别是同步异常和异步异常。
同步异常是由于执行或尝试执行某条指令触发的。
异步异常包括通常意义上的中断(如外设产生的中断信号,在 ARMv8 上即 FIQ/IRQ 信号)以及系统错误(SError)。

System errors have several possible causes, the most common being asynchronous Data Aborts (for example, an abort that is triggered by write-back of dirty data from a cache line to external memory)

Exception entry

On taking an exception to AArch64 state,发生异常时根据异常的种类,有不同的行为。

程序状态字 SPSR 会保存到异常处理的目标异常级别 SPSR_ELx
返回地址则保存到对应的 ELR_ELx
自动屏蔽中断。All of PSTATE {D, A, I, F} are set to 1.
堆栈指针切换到对应的 SP_ELx
某些异常会保存出错的虚拟地址到 FAR_ELx

• An Instruction Abort exception.
• A Data Abort exception.
• A PC alignment fault exception.
• A Watchpoint exception.
...

之后执行流进入异常向量表里指定的位置开始执行。

Execution moves to the target Exception level, and starts at the address defined by the exception vector. Which exception vector is used is also an indicator of whether the exception came from a lower Exception level or the current Exception level.

Exception return

  • For asynchronous exceptions, it is the address of the instruction following the instruction boundary at which the interrupt occurs. Therefore, it is the address of the first instruction that did not execute, or did not complete execution, as a result of taking the interrupt. 由于发生中断而未执行或未完成执行的第一条指令的地址。
  • For synchronous exceptions other than system calls, it is the address of the instruction that generates the exception. 不包括 syscall,指令自身地址。
  • For exception generating instructions, it is the address of the instruction that follows the exception generating instruction. 对于异常产生指令(svc,hvc,smc),是该指令的下一条指令地址。

Data Model

64-bit Linux使用 LP64,ARM的A64指令集支持 LP64LLP64Procedure Call Standard (PCS)
在这里插入图片描述
关于数据模型,更详细的介绍可参看数据模型。

CTLR reg

SCTLR_EL3.NS

Non-secure bit.
0 Indicates that EL0 and EL1 are in Secure state, and so memory accesses from those
Exception levels can access Secure memory.1 Indicates that EL0 and EL1 are in Non-secure state, and so memory accesses from those
Exception levels cannot access Secure memory.EL2 is not supported in the Secure state. When SCR_EL3.NS==0, it is not possible to enter EL2,
and the EL2 state has no effect on execution.

Hypervisor Configuration Register

HCR_EL2.TGE Trap General Exceptions, from Non-secure EL0.

0 This control has no effect on execution at EL0.
1 When the value of SCR_EL3.NS is 0, this control has no effect on execution at EL0.
When the value of SCR_EL3.NS is 1, in all cases:
• All exceptions that would be routed to EL1 are routed to EL2.
• The SCTLR_EL1.M field, or the SCTLR.M field if EL1 is using AArch32, is
treated as being 0 for all purposes other than returning the result of a direct read
of SCTLR_EL1 or SCTLR.
• All virtual interrupts are disabled.
• Any IMPLEMENTATION DEFINED mechanisms for signaling virtual interrupts are
disabled.
• An exception return to EL1 is treated as an illegal exception return.
When the value of SCR_EL3.NS is 1 and the value of HCR_EL2.E2H is 0, additionally:
• The HCR_EL2.{FMO, IMO, AMO} fields are treated as being 1 for all purposes
other than a direct read or write access of HCR_EL2.
• The MDCR_EL2.{TDRA,TDOSA,TDA, TDE} fields are treated as being 1 for
all purposes other than returning the result of a direct read of MDCR_EL2.
For information on the behavior of this bit when E2H is 1, see Behavior of
HCR_EL2.E2H on page D4-2183.
HCR_EL2.TGE must not be cached in a TLB.
In an implementation that includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves as if
this field is 0 for all purposes other than a direct read or write access of HCR_EL2.

http://chatgpt.dhexx.cn/article/By9lceoH.shtml

相关文章

V8引擎解析JavaScript原理

为什么需要JavaScript引擎呢? 高级的编程语言都是需要转成最终的机器指令执行的 我们编写的JavaScript无论交给浏览器和Node执行,最后都是被CPU执行的 CPU只认识自己的指令集,实际上是机器语言,才可以被CPU所执行 所以需要借助Jav…

V8引擎详解(一)——概述

背景 在现有的javascript引擎中,V8引擎绝对是其中的佼佼者,chrome和node底层都使用了V8引擎,其中chrome的市场占有率已经达到70%,而node更是前端工程化以及扩展边界的核心支柱,V8引擎对于一个前端开发工程师来说重要程…

浏览器工作原理和V8引擎

一、浏览器的工作原理 比如在浏览器中输入网址,然后dns进行解析,解析出的就是服务器的一个ip地址。服务器返回一个html文件,浏览器内核在解析html文件的过程中,遇到link标签和script标签引用的css文件和JavaScript文件就会去下载…

V8引擎学习

计算机模型 寄存器 中央处理器的组成部分寄存器是有限存储容量额高速存储部件可以用来暂存指令,数据和地址存储器内的数据可以用来执行算术和逻辑运算。寄存器内的地址可用于指向内存的某个位置 内存 随机存取存储器也叫内存,英文缩写RAMRAM是与CPU直…

V8垃圾回收

来自李兵老师的《浏览器工作原理与实践》,太赞了 垃圾回收 内存空间栈空间和堆空间不同语言的垃圾回收策略调用栈中的数据是如何回收的堆中的数据是如何回收的副垃圾回收器主垃圾回收器总结 在我们说V8垃圾回收之前,先讲讲 数据是如何存储的?…

认识V8引擎

1、前言 编程语言一般分为两类,解释性语言和编译性语言。编译型语言在执行之前要先进行完全编译,而解释型语言一边编译一边执行,很明显编译型语言会比解释性语言快,而JavaScript就是一种解释型脚本语言,支持动态类型、…

V8引擎执行原理

v8是C编写的Google开源高性能JavaScript和WebAssembly引擎,它用于Chrome和Node.js等。 它实现ECMAScript和WebAssembly。 v8可独立运行,也可嵌入到任何C应用程序中。 parse模块 parse模块会将JavaScript代码转换成AST(抽象语法树),因为…

Google V8引擎浅析

前端开发人员都会遇到一个流行词:V8。它的流行程度很大一部分是因为它将JavaScript的性能提升到了一个新的水平。是的,V8很快。但它是如何发挥它的魔力? 前言 源代码:https://source.chromium.org/chromium/chromium/src//master:…

js中v8引擎的详解-看的吐血

v8引擎出现的原因 这里先说一下什么是编译型语言和解释性语言: 编译型语言: 在程序执行之前必须进行专门的编译过程,有如下特点: 只须编译一次就可以把源代码编译成机器语言,后面的执行无须重新编译,直接…

Google V8 引擎

V8的前世今生 V8是JavaScript渲染引擎,第一个版本随着Chrome的发布而发布(具体时间为2008年9月2日)。在运行JavaScript之前,相比其它的JavaScript的引擎转换成字节码或解释执行,V8将其编译成原生机器码(IA-32, x86-64, ARM, or M…

编译v8引擎

本机环境是win7vs2010(本来想用2012的,但是发现默认的项目是2010的) 1、首先下载V8的源码 安装好svn,并在cmd下能使用svn help: svn下载地址(安装包):http://download.csdn.net/detail/zengraoli/5651551 …

JavaScript引擎—V8引擎

为什么需要Javascript引擎? 随着JS承担的工作越来越多,早就已超越创造出的初衷(表单验证)的范畴,因此需要快速的解析和执行JavaScript脚本 V8引擎由此而生 JavaScript引擎主要功能:结合JS语言特性 和 本质 …

LVGL V8

本文适用于LVGL V8版本 LVGL simulator vs2019 官方工程 lv_sim_visual_studio 使用注意事项: 1、将官方工程从github上下载下来,最好使用git 将整个工程clone下来,因为工程内部有依赖,如果只是将工程Download下来,无…

为什么V8引擎这么快?

转载请注明出处:http://blog.csdn.net/horkychen Google研发的V8 JavaScript引擎性能优异。我们请熟悉内部程序实现的作者依源代码来看看V8是如何加速的。 作者:Community Engine公司研发部研发工程师Hajime Morita Google的Chrome中的V8 JavaScript引擎&#xff0…

垃圾回收机制之v8引擎

v8的内存分配 (栈(执行环境)跟堆) 堆内存负责垃圾回收机制,只有新生代和老生代两部分 新生代:对等分的(严格) 老生代: 都是由新生代转变的(连续的空间&…

V8 JavaScript引擎

简介 V8 (v8.dev)是 Google 的开源高性能 JavaScript 和 WebAssembly 引擎,用 C 编写。它用于 Chrome 和 Node.js 等。它实现了 ECMAScript 和 WebAssembly,并运行在 Windows 7 或更高版本、macOS 10.12 以及使用 x64、IA-32、ARM 或 MIPS 处理器的 Lin…

V8、JSCore、Hermes、QuickJS,hybrid开发JS引擎怎么选

📌 如果你喜欢我写的文章,可以把我的公众号设为星标 🌟,这样每次有更新就可以及时推送给你啦 在一般的移动端开发场景中,每次更新应用功能都是通过 Native 语言开发并通过应用市场版本分发来实现的。 但是市场瞬息万变…

v8引擎详解

前言 JavaScript绝对是最火的编程语言之一,一直具有很大的用户群,随着在服务端的使用(NodeJs),更是爆发了极强的生命力。编程语言分为编译型语言和解释型语言两类,编译型语言在执行之前要先进行完全编译&am…

Chrome V8引擎介绍

随着Web相关技术的发展,JavaScript所要承担的工作也越来越多,早就超越了“表单验证”的范畴,这就更需要快速的解析和执行JavaScript脚本。V8引擎就是为解决这一问题而生,在node中也是采用该引擎来解析JavaScript。V8是如何使得Jav…

V8引擎原理

V8引擎原理 V8是用C编写的Googl开源高性能JavaScript和WebAssembly引擎,它也用于Chrome和Node.js等 V8的解析js的流程 js直接放到cpu中无法执行,需要通过v8转换js先被转换成ast语法树,在此期间主要是进行词法分析和语法分析ast语法树通过…