【ARM 嵌入式 C 入门及渐进 6 -- UEFI中使用ASM_PFX在汇编中调用C代码 】

UEFI ASM_PFX 简介

在UEFI中,asm_pfx是一个宏定义,用于指定汇编代码中的前缀。它的作用是为了在不同的编译器和不同的体系结构上生成正确的汇编代码。具体来说,asm_pfx会根据编译器和体系结构的不同,生成不同的前缀,以确保汇编代码的正确性和可移植性。在UEFI中,asm_pfx 通常用于定义汇编代码中的指令,例如 movjmp 等指令。

UEFI中的汇编代码,目前支持的有 .S.asm.nasm 格式的汇编:

  • 第一个是 AT&T 汇编;
  • 后两个是 Intel 汇编,只是使用的汇编样式稍有不同。

在 ARM 体系结构中,asm_pfx宏通常用于定义汇编代码中的指令前缀。例如,在ARMv8架构中,asm_pfx可以被定义为 “.arch armv8-a”,以确保汇编代码能够正确地在ARMv8处理器上运行。此外,asm_pfx 还可以用于定义其他指令前缀,例如".thumb"或".fpu neon",以支持不同的指令集和处理器特性。总之,asm_pfx在UEFI中的使用与在ARM中的使用是相似的,都是用于确保汇编代码的正确性和可移植性。

UEFI 汇编调用C函数

如下代码所示,在汇编代码中可以使用下面语句调用C文件中读去CPU ID的函数:

//el1_entry:
el1_entry_func:
  bl    ASM_PFX(get_cpu_id)
  • 读取 CPU ID 的函数实现如下:
uint64_t get_cpu_id(void)
{
        uint64_t mpidr_el1;

        asm volatile("mrs %0, mpidr_el1" : "=r" (mpidr_el1));

        return mpidr_el1;
}

UEFI 汇编调用带参数的C函数

  • 一、汇编代码传一个参数给C函数
  mrs x0, mpidr_el1
  ldr   x4, =ASM_PFX(debug_info)
  blr   x4
  • dfd_info 函数实现如下:
void debug_info(uint64_t val)
{
        Print(L"---line:%d val:0x%016x ---\n", __LINE__, val);
}
  • 二、汇编代码传2个参数给C函数
    以判断Armv8/v9 当前系统所处EL为例,在ARMv9架构中,可以通过读取CurrentEL寄存器的值来确定系统当前所处的EL级别,例如,如果CurrentEL寄存器的值为0x2,则表示系统当前处于EL2,要访问它,可以使用MRS(Move to Register from System)指令,将其值加载到通用寄存器中。例如,使用以下汇编代码可以将CurrentEL寄存器的值加载到x0寄存器中:
  MOV x0, #0
  MOV x1, #1
  MRS x0, CurrentEL
  ldr   x4, =ASM_PFX(dfd_info)
  blr   x4

对应的 debub_info 函数实现如下:

void dfd_info(uint64_t val, uint64_t val1)
{
        Print(L"---line:%d val:0x%016x mark:%d---\n", __LINE__, val, val1);
}
CurrentEL 寄存器:

在这里插入图片描述

UEFI 添加头文件搜索路径

需要再对应的 dec 文件中添加include目录,以edk2/ArmPkg/ArmPkg.dec为例:

 Library/ArmLib/test/include

这样 test 目录下的include 目录会添加到头文件的搜索路径中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
This Unified Extensible Firmware Interface (hereafter known as UEFI) Specification describes an interface between the operating system (OS) and the platform firmware. UEFI was preceded by the Extensible Firmware Interface Specification 1.10 (EFI). As a result, some code and certain protocol names retain the EFI designation. Unless otherwise noted, EFI designations in this specification may be assumed to be part of UEFI. The interface is in the form of data tables that contain platform-related information, and boot and runtime service calls that are available to the OS loader and the OS. Together, these provide a standard environment for booting an OS. This specification is designed as a pure interface specification. As such, the specification defines the set of interfaces and structures that platform firmware must implement. Similarly, the specification defines the set of interfaces and structures that the OS may use in booting. How either the firmware developer chooses to implement the required elements or the OS developer chooses to make use of those interfaces and structures is an implementation decision left for the developer. The intent of this specification is to define a way for the OS and platform firmware to communicate only information necessary to support the OS boot process. This is accomplished through a formal and complete abstract specification of the software-visible interface presented to the OS by the platform and firmware. Using this formal definition, a shrink-wrap OS intended to run on platforms compatible with supported processor specifications will be able to boot on a variety of system designs without further platform or OS customization. The definition will also allow for platform innovation to introduce new features and functionality that enhance platform capability without requiring new code to be written in the OS boot sequence. Furthermore, an abstract specification opens a route to replace legacy devices and firmware code over time. New device types and associated code can provide equivalent functionality through the same defined abstract interface, again without impact on the OS boot support code. The specification is applicable to a full range of hardware platforms from mobile systems to servers. The specification provides a core set of services along with a selection of protocol interfaces. The selection of protocol interfaces can evolve over time to be optimized for various platform market segments. At the same time, the specification allows maximum extensibility and customization abilities for OEMs to allow differentiation. In this, the purpose of UEFI is to define an evolutionary path from the traditional “PC-AT”- style boot world into a legacy-API free environment.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

主公CodingCos

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值