👹
Carlos's Tech Blog
  • 🧔ECUs
    • ZYNQ_Documents
      • [ZYNQ] 构建ZYNQ的BSP工程
      • [ZYNQ] 启动流程
      • [ZYNQ] Secure Boot Flow
      • [ZYNQ] Provisioning Guideline
      • [ZYNQ] Decrypting Partition by the Decrypt Agent Using PUF key
      • [ZYNQ] enabling the cryptsetup on ramdisk
      • [ZYNQ] Encrypt external files based on file system using PUF key
      • [ZYNQ] Loading an Encrypted Linux kernel at U-Boot with a KUP Key
      • [ZYNQ] cross-compile the cryptsetup on Xilinx ZYNQ aarch64 platform
      • [ZYNQ] Linux Linaro系统镜像制作SD卡启动
    • S32G_Documents
      • [S32G] Going through the s32g hard/soft platform
      • [S32G] S32g247's Secure Boot using HSE firmware
        • S32g2 HSE key config
        • How S32g verify secure boot image
        • S32g secure boot signature generation
        • How to download and build S32g Secure boot image
        • [S32G] OTA with Secure Boot
    • RT117x_Documents
      • [RT-117x]IMX RT1170 Provisioning Guideline
      • [RT-117x] Going through the MX-RT1170 hard/soft platform
      • [RT-117x] i.MX-RT1170's Secure Boot
        • [RT-117x]Signing image with the HSM (SignServer)
    • LS104x_Documents
      • [LS104x] bsp project
      • [LS104x] boot flow
      • [LS104x] secure boot
      • [LS104x] Application Note, Using the PKCS#11 in TCU platform
      • [LS104x] 使用ostree更新rootfs
      • [LS104x] ostree的移植
      • [LS104x] Starting with Yocto
      • [LS104x] 使用FIT的kernel格式和initramfs
    • IMX6/8_Documents
      • [IMX6] Defining A U-Boot Command
      • NXP IMX6 嵌入式板子一些笔记
      • NXP-imx6 initialization
    • Vehicle_Apps
      • [SecOC] Tree
        • [SecOC] SecOC Freshness and MAC Truncation
  • 😾TECH
    • Rust Arm OS
      • ARMv7m_Using_The_RUST_Cross_Compiler
    • ARM
      • ARM-v7-M
        • 01_ARMv7-M_处理器架构技术综述
        • 02_ARMv7-M_编程模型与模式
        • 03_ARMv7-M_存储系统结构
        • 04_ARMv7-M_异常处理及中断处理
      • ARM-v8-A
        • 02_ARMv8_基本概念
        • 03_ARMv8_指令集介绍_加载指令集和存储指令集
        • 04_ARMv8_指令集_运算指令集
        • 05_ARMv8_指令集_跳转_比较与返回指令
        • 06_ARMv8_指令集_一些重要的指令
        • 0X_ARMv8_指令集_基于汇编的UART驱动
        • 07_ARMv8_汇编器Using as
        • 08_ARMv8_链接器和链接脚本
        • 09_ARMv8_内嵌汇编(内联汇编)Inline assembly
        • 10_ARMv8_异常处理(一) - 入口与返回、栈选择、异常向量表
        • 11_ARMv8_异常处理(二)- Legacy 中断处理
        • 12_ARMv8_异常处理(三)- GICv1/v2中断处理
        • 13_ARMv8_内存管理(一)-内存管理要素
        • 14_ARMv8_内存管理(二)-ARM的MMU设计
        • 15_ARMv8_内存管理(三)-MMU恒等映射及Linux实现
        • 16_ARMv8_高速缓存(一)cache要素
        • 17_ARMv8_高速缓存(二)ARM cache设计
        • 18_ARMv8_高速缓存(三)多核与一致性要素
        • 19_ARMv8_TLB管理(Translation Lookaside buffer)
        • 20_ARMv8_barrier(一)流水线和一致性模型
        • 21_ARMv8_barrier(二)内存屏障案例
      • ARM Boot Flow
        • 01_Embedded_ARMv7/v8 non-secure Boot Flow
        • 02_Embedded_ARMv8 ATF Secure Boot Flow (BL1/BL2/BL31)
        • 03_Embedded_ARMv8 BL33 Uboot Booting Flow
      • ARM Compiler
        • Compiler optimization and the volatile keyword
      • ARM Development
        • 在MACBOOK上搭建ARMv8架构的ARM开发环境
        • Starting with JLink debugger or QEMU
    • Linux
      • Kernel
        • 0x01_LinuxKernel_内核的启动(一)之启动前准备
        • 0x02_LinuxKernel_内核的启动(二)SMP多核处理器启动过程分析
        • 0x21_LinuxKernel_内核活动(一)之系统调用
        • 0x22_LinuxKernel_内核活动(二)中断体系结构(中断上文)
        • 0x23_LinuxKernel_内核活动(三)中断体系结构(中断下文)
        • 0x24_LinuxKernel_进程(一)进程的管理(生命周期、进程表示)
        • 0x25_LinuxKernel_进程(二)进程的调度器的实现
        • 0x26_LinuxKernel_设备驱动(一)综述与文件系统关联
        • 0x27_LinuxKernel_设备驱动(二)字符设备操作
        • 0x28_LinuxKernel_设备驱动(三)块设备操作
        • 0x29_LinuxKernel_设备驱动(四)资源与总线系统
        • 0x30_LinuxKernel_设备驱动(五)模块
        • 0x31_LinuxKernel_内存管理(一)物理页面、伙伴系统和slab分配器
        • 0x32_LinuxKernel_内存管理(二)虚拟内存管理、缺页与调试工具
        • 0x33_LinuxKernel_同步管理_原子操作_内存屏障_锁机制等
        • 01_LinuxDebug_调试理论和基础综述
      • Userspace
        • Linux-用户空间-多线程与同步
        • Linux进程之间的通信-管道(上)
        • Linux进程之间的通信-管道(下)
        • Linux进程之间的通信-信号量(System V)
        • Linux进程之间的通信-内存共享(System V)
        • Linux进程之间的通信-消息队列(System V)
        • Linux应用调试(一)方法、技巧和工具 - 综述
        • Linux应用调试(二)工具之coredump
        • Linux应用调试(三)工具之Valgrind
        • Linux机制之内存池
        • Linux机制之对象管理和引用计数(kobject/ktype/kset)
        • Linux机制copy_{to, from}_user
        • Linux设备树 - DTS语法、节点、设备树解析等
        • Linux System : Managing Linux Services - inittab & init.d
        • Linux System : Managing Linux Services - initramfs
      • Kernel Examples
        • Linux Driver - GPIO键盘驱动开发记录_OMAPL138
        • 基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(一)之miscdevice和ioctl
        • 基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(二)之cdev与read、write
        • 基于OMAPL138的字符驱动_GPIO驱动AD9833(三)之中断申请IRQ
        • Linux内核调用SPI驱动_实现OLED显示功能
        • Linux内核调用I2C驱动_驱动嵌套驱动方法MPU6050
    • OPTEE
      • 01_OPTEE-OS_基础之(一)功能综述、简要介绍
      • 02_OPTEE-OS_基础之(二)TrustZone和ATF功能综述、简要介绍
      • 03_OPTEE-OS_系统集成之(一)编译、实例、在QEMU上执行
      • 05_OPTEE-OS_系统集成之(三)ATF启动过程
      • 06_OPTEE-OS_系统集成之(四)OPTEE镜像启动过程
      • 07_OPTEE-OS_系统集成之(五)REE侧上层软件
      • 08_OPTEE-OS_系统集成之(六)TEE的驱动
      • 09_OPTEE-OS_内核之(一)ARM核安全态和非安全态的切换
      • 10_OPTEE-OS_内核之(二)对安全监控模式的调用的处理
      • 11_OPTEE-OS_内核之(三)中断与异常的处理
      • 12_OPTEE-OS_内核之(四)对TA请求的处理
      • 13_OPTEE-OS_内核之(五)内存和cache管理
      • 14_OPTEE-OS_内核之(六)线程管理与并发
      • 15_OPTEE-OS_内核之(七)系统调用及IPC机制
      • 16_OPTEE-OS_应用之(一)TA镜像的签名和加载
      • 17_OPTEE-OS_应用之(二)密码学算法和安全存储
      • 18_OPTEE-OS_应用之(三)可信应用的开发
      • 19_OPTEE-OS_应用之(四)安全驱动开发
      • 20_OPTEE-OS_应用之(五)终端密钥在线下发系统
    • Binary
      • 01_ELF文件_目标文件格式
      • 02_ELF文件结构_浅析内部文件结构
      • 03_ELF文件_静态链接
      • 04_ELF文件_加载进程虚拟地址空间
      • 05_ELF文件_动态链接
      • 06_Linux的动态共享库
      • 07_ELF文件_堆和栈调用惯例以ARMv8为例
      • 08_ELF文件_运行库(入口、库、多线程)
      • 09_ELF文件_基于ARMv7的Linux系统调用原理
      • 10_ELF文件_ARM的镜像文件(.bin/.hex/.s19)
    • Build
      • 01_Script_makefile_summary
    • Rust
      • 02_SYS_RUST_文件IO
    • Security
      • Crypto
        • 1.0_Security_计算机安全概述及安全需求
        • 2.0_Security_随机数(伪随机数)
        • 3.0_Security_对称密钥算法加解密
        • 3.1_Security_对称密钥算法之AES
        • 3.2_Security_对称密钥算法之MAC(CMAC/HMAC)
        • 3.3_Security_对称密钥算法之AEAD
        • 8.0_Security_pkcs7(CMS)_embedded
        • 9.0_Security_pkcs11(HSM)_embedded
      • Tools
        • Openssl EVP to implement RSA and SM2 en/dec sign/verify
        • 基于Mac Silicon M1 的OpenSSL 编译
        • How to compile mbedtls library on Linux/Mac/Windows
    • Embedded
      • eMMC启动介质
  • 😃Design
    • Secure Boot
      • JY Secure Boot Desgin
    • FOTA
      • [FOTA] Module of ECUs' FOTA unit design
        • [FOTA] Tech key point: OSTree Deployment
        • [FOTA] Tech key point: repositories role for onboard
        • [FOTA] Tech key point: metadata management
        • [FOTA] Tech key point: ECU verifying and Decrpting
        • [FOTA] Tech key point: time server
      • [FOTA] Local-OTA for Embedded Linux System
    • Provisioning
      • [X-Shield] Module of the Embedded Boards initialization
    • Report
由 GitBook 提供支持
在本页
  • 05-ARMv8-指令集-跳转和比较指令
  • 1. CLZ
  • 2. 比较指令
  • 3. 条件选择指令
  • 4. 跳转与返回指令
  • Ref
  1. TECH
  2. ARM
  3. ARM-v8-A

05_ARMv8_指令集_跳转_比较与返回指令

https://github.com/carloscn/blog/issues/10

05-ARMv8-指令集-跳转和比较指令

  • 零计数指令CLZ

  • 比较指令:CMP, CMN

  • 跳转指令:B, BR, BL, BLR

  • 条件选择指令:CSEL, CSET, CSINC

1. CLZ

计算最高为1的比特位前面有多少个0。例如, 0x0800 0000 0000 000F,前面是有4个0的(使用64位的寄存器),如果使用Wn寄存器,按照32位算。

  • CLZ

    • Define: CLZ <Xd>, <Xn>

    • Example1: clz x0, x1 [计算x1寄存器内的值最高位1的比特位前面有多少个0,并放入x0]

2. 比较指令

比较指令有CMP和CMN,CMP本质为:CMP x1, x2 -> x1 = x1 - x2;CMN为负向比较:CMN x1, x2->x1 = x1 + x2。

2.1 CMP

NZC = a - b,根据不同的比较结果,来确定NZC的标志位,若x1 > x2, NCZV = 0100,若x1 = x2, NCZV = 0110,若x1 < x2, NCZV = 1000。

  • CMP (immediate):

    • Define: CMP <Xd|SP>, #<imm>{, lsl <#shift>}, note shift supports #0 and #12 only.

    • Example1: cmp x1, #8 (x1 = x2 - 8)

    • Example2: cmp x1, #8, lsl #12 ( x1 = x2 - (8 << 12) )

  • CMP (shifted register):

    • Define: CMP <Xd>, <Xm>{, <shift> #<amount>} , note #amount range 0 to 63

    • Note: LSL when shift = 0, LSR when shift = 1, ASR when shift = 2

    • Example1: cmp x1, x2, asr #2

2.1 CMN

NZC = a + b,a和b如果加和为负数,N置位; a + b的和为0,Z置位;a + b溢出(两个负数相加),C置位。

  • CMN (extended register) :

    • Define: CMN <Xd|SP>, <R><m>, {<extend> {#<amount>}}

    • Example1: cmn x0, x1 ( x0 = x0 - x1 )

    • Example2: cmn x0, x1, lsl #5( x0 = x0 - (x1 << 5) )

  • CMN (immediate):

    • Define: CMN <Xd|SP>, #<imm>{, lsl <#shift>}, note shift supports #0 and #12 only.

    • Example1: cmn x1, #8 (x1 = x2 - 8)

    • Example2: cmn x1, #8, lsl #12 ( x1 = x2 - (8 << 12) )

  • CMN (shifted register):

    • Define: CMN <Xd>, <Xm>{, <shift> #<amount>} , note #amount range 0 to 63

    • Note: LSL when shift = 0, LSR when shift = 1, ASR when shift = 2

    • Example1: cmn x1, x2, asr #2

对于CMN,根据实验有以下几种情况:

Condition
Algo
N
Z
C

-0x01 > -0x0E

(-0x01) + (- 0x0E) = -0x0F

1

0

1

0 = 0

0 + 0 = 0

0

1

0

-0x0F < -0x01

(-0x0F) + (-0x01) = -0x10

1

0

1

0x0F > -0x01

(0x0F) + (-0x01) = 0x0E

0

0

1

-0x0F < 0x01

(-0x0F) + (0x01) = -0x0E

1

0

0

-0x01 < 0x01

(-0x01) + 0x01 = 0

0

1

1

2.3 Condition Codes

(CMP and CMN) Sets the condition flags to the result of a comparison if the original condition is true. If not true, the conditional flags are set to a specified condition flag state. The conditional compare instruction is very useful for expressing nested or compound comparisons.

Condition codes:

Code
Encoding
Meaning (when set by CMP)
Meaning (when set by FCMP)
Condition flags

EQ

0b0000

Equal to.

Equal to.

Z =1

NE

0b0001

Not equal to.

Unordered, or not equal to.

Z = 0

CS

0b0010

Carry set (identical to HS).

Greater than, equal to, or unordered (identical to HS).

C = 1

HS

0b0010

Greater than, equal to (unsigned) (identical to CS).

Greater than, equal to, or unordered (identical to CS).

C = 1

CC

0b0011

Carry clear (identical to LO).

Less than (identical to LO).

C = 0

LO

0b0011

Unsigned less than (identical to CC).

Less than (identical to CC).

C = 0

MI

0b0100

Minus, Negative.

Less than.

N = 1

PL

0b0101

Positive or zero.

Greater than, equal to, or unordered.

N = 0

VS

0b0110

Signed overflow.

Unordered. (At least one argument was NaN).

V = 1

VC

0b0111

No signed overflow.

Not unordered. (No argument was NaN).

V = 0

HI

0b1000

Greater than (unsigned).

Greater than or unordered.

(C = 1) && (Z = 0)

LS

0b1001

Less than or equal to (unsigned).

Less than or equal to.

(C = 0) || (Z = 1)

GE

0b1010

Greater than or equal to (signed).

Greater than or equal to.

N==V

LT

0b1011

Less than (signed).

Less than or unordered.

N!=V

GT

0b1100

Greater than (signed).

Greater than.

(Z==0) && (N==V)

LE

0b1101

Less than or equal to (signed).

Less than, equal to or unordered.

(Z==1) || (N!=V)

AL

0b1110

Always executed.

Default. Always executed.

Any

NV

0b1111

Always executed.

Always executed.

Any

// The test is:
// suppose the x1 = 1, x2 = -3
// Use the `cmn` instruction to compare the x1 and x2
// When the result is neg number, make x2 += 1;
// until the result is zero, then return the function.
test_cmn_jump:
	msr NZCV, xzr
	mov x0, xzr
	mov x1, #0x0
loop:
	add x1, x1, #0x1
	mov x2, #-0x3
	cmn x1, x2		// NZCV = 1000
	mrs x0, NZCV
	b.mi loop
	ret

3. 条件选择指令

条件选择指令包括CSEL\CSET\CSINC三条指令,也是非常重要的指令。

3.1 CSEL

CSEL, 条件选择指令,如果cond为真,那么xd就是xn的值,否则就是xm的值。注意,为上面表格的值。

  • Define: CSEL <Xd>, <Xn>, <Xm>, <cond>

  • Example1: csel x1, x1, x2, EQ -> 如果Z=1,x1维持原始值,否则x2给x1赋值

  • Example2: csel x1, x1, x2, MI -> 如果N=1,x1维持原始值,否则x2给x1赋值

3.2 CSET

CSET, 条件置位指令,如果cond为真,那么xd就是1,否则就是0。注意,为上面表格的值。

  • Define: CSET <Xd>, <cond>

  • Example1: cset x1, EQ -> 如果Z=1,x1是1, 否则为0

  • Example2: cset x1, MI -> 如果N=1,x1是1, 否则为0

3.3 CSINC

CSINC,条件选择并增加指令 如果cond为真,那么xd就是xn的值,否则就是xm+1的值。注意,为上面表格的值。

  • Define: CSINC <Xd>, <Xn>, <Xm>, <cond>

  • Example1: csinc x1, x1, x2, EQ -> 如果Z=1,x1维持x1, 否则为x2+1

  • Example2: csinc x1, x2, x3, MI -> 如果N=1,x1为x2, 否则为x3+1

3.4 Example

用汇编实现下面的c代码:

unsigned long cel_test(unsigned long a, unsigned long b)
{
  if (a == 0) {
    return b + 2;
  } else {
    return b - 1;
  }
}

分析拆解:

  • 比较指令 a与0 的比较,a为0的时候 Z标志位可以使用,因此可以利用Z标识位条件EQ作为该分支的入口。

  • b + 2和b-1 分支分别有个运算操作数的动作,确定 +2 还是 -1 可以把 2和 -1 放在一个寄存器里面,由 csel来判断那个值。

test_csel:

	mov x2, #0x2
	mov x3, #-0x1
	cmp x0, #0
	csel x4, x2, x3, EQ
	add x0, x1, x4
	ret

4. 跳转与返回指令

  • B: b跳转指令, b 可以跳到PC ±128MB的范围, 不返回

  • B. : 使用跳转指令b.<cond>,xx为以上表格的指,不返回

  • BR: 跳转到寄存器指定的地址处,不返回

  • BL:带返回地址的,PC±128MB, 用于跳转到子函数,返回地址为PC+4,设置到X30寄存器中。

  • BLX: 跳转到寄存器指定的地址处,可以返回。返回地址保存到X30寄存器,保存的是父函数的PC+4

  • RET: 从子函数返回,通常用X30里保存的返回地址返回。

  • ERET:从当前的异常模式返回,通常可以实现模式切换,例如EL1切换到EL0。它会从SPSR恢复PSTATE,从ELR中获取跳转地址,并返回到该地址。

Example:

  • 新建一个汇编文件

  • 创建一个bl_test的汇编函数,在该汇编函数中使用bl指令来跳转到csel_test汇编函数中

  • 在kernel.c文件中,C语言调用该bl_test汇编函数。

分析:

调用顺序应该是main -----> bl_test -----> csel_test ---> ret, 如果使用bl指令跳转到csel_test中,csel_test返回之后 bl_test拿到的是csel_test返回地址PC,而且X30寄存器被修改为csel_test子函数的地址,如果把PC+4返回给main函数,main函数看到的是当前的PC+4,肯定是错误的。这个难点就在于bl跳转子函数之后的返回需要处理好,重置X30寄存器的地址的值。

test_csel:

	mov x2, #0x2
	mov x3, #-0x1
	cmp x0, #0
	csel x4, x2, x3, EQ
	add x0, x1, x4
	ret

test_bl:
	mov x8, x30    // 备份main函数call进来x30的lr的值,否则到这里函数回不去
	mov x0, 1
	mov x1, 3
	bl test_csel   // 跳转进入这个函数之后x30寄存器被test_csel子函数冲走了
	mov x30, x8    // 恢复x30的值,此时ret之后可以回到main函数的地址继续执行
	retå

Ref

上一页04_ARMv8_指令集_运算指令集下一页06_ARMv8_指令集_一些重要的指令

最后更新于1年前

😾