CN119806544B - Binary translation method, binary translation device, electronic equipment and readable storage medium - Google Patents
Binary translation method, binary translation device, electronic equipment and readable storage mediumInfo
- Publication number
- CN119806544B CN119806544B CN202510293378.3A CN202510293378A CN119806544B CN 119806544 B CN119806544 B CN 119806544B CN 202510293378 A CN202510293378 A CN 202510293378A CN 119806544 B CN119806544 B CN 119806544B
- Authority
- CN
- China
- Prior art keywords
- instruction
- address
- memory
- memory access
- access address
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
Landscapes
- Memory System Of A Hierarchy Structure (AREA)
Abstract
本申请公开了一种二进制翻译方法、装置、电子设备及可读存储介质,方法包括:从翻译器的缓存中获取每个基本块的翻译结果;在执行所述翻译结果中的指令时,若确定当前执行的指令为存在访存对齐异常的目标指令,则根据所述目标指令中包括的第一访存地址进行重新映射,生成第二访存地址,并将所述第一访存地址所对应的内存片区配置为不可访问;针对所述第二访存地址执行访存操作,以访问所述第二访存地址的数据。可以解决在先技术中无条件停止所有其他线程的执行,导致影响源程序指令翻译执行的效率问题。
This application discloses a binary translation method, apparatus, electronic device, and readable storage medium. The method includes: obtaining the translation result of each basic block from the translator's cache; when executing an instruction in the translation result, if it is determined that the currently executed instruction is a target instruction with a memory alignment exception, remapping is performed based on a first memory access address included in the target instruction to generate a second memory access address, and the memory slice corresponding to the first memory access address is configured as inaccessible; and a memory access operation is performed on the second memory access address to access the data at the second memory access address. This method can solve the problem in the prior art of unconditionally stopping the execution of all other threads, which affects the efficiency of source program instruction translation execution.
Description
技术领域Technical Field
本申请属于计算机技术领域,具体涉及一种二进制翻译方法、装置、电子设备及可读存储介质。The present application belongs to the field of computer technology, and specifically relates to a binary translation method, device, electronic device, and readable storage medium.
背景技术Background Art
二进制翻译可以将一种指令集架构(ISA,Instruction Set Architecture)的指令转换为另一种ISA的指令。宿主机平台在对客户机平台的指令进行翻译时,若处理指令对应的访存地址不满足宿主平台支持的访问对齐状态的要求,通常会将一个非对齐访存操作拆分成多个对齐访存操作来执行,但是如果采用这种方式来模拟客户机中的原子指令,由于拆分后的多个访存操作无法满足指令的原子性,在操作过程中往往会被其他线程或进程打断。Binary translation converts instructions from one instruction set architecture (ISA) into instructions from another ISA. When the host platform translates instructions from the client platform, if the memory access address corresponding to the processing instruction does not meet the access alignment requirements supported by the host platform, the host platform typically splits an unaligned memory access operation into multiple aligned memory access operations. However, if this approach is used to simulate atomic instructions in the client, the multiple memory access operations cannot meet the atomicity requirements of the instruction and are often interrupted by other threads or processes during the operation.
相关技术中,在上述情况下,为了避免在操作过程中会被其他线程或进程打断,通常会采取在执行这条指令时使其它线程都停下来的方式,来保证客户机中的原子指令在翻译后执行时的原子性,但是采用这种方式,无条件停止所有其他线程的执行,会影响源程序指令翻译执行的效率。In the related art, in the above situation, in order to avoid being interrupted by other threads or processes during the operation, it is usually adopted to stop other threads when executing this instruction to ensure the atomicity of the atomic instruction in the client when it is executed after translation. However, using this method to unconditionally stop the execution of all other threads will affect the efficiency of the translation and execution of the source program instructions.
发明内容Summary of the Invention
本申请旨在提供一种二进制翻译方法、装置、电子设备及可读存储介质,至少解决在先技术中无条件停止所有其他线程的执行,导致影响源程序指令翻译执行的效率问题。The present application aims to provide a binary translation method, device, electronic device and readable storage medium, which at least solves the problem in the prior art of unconditionally stopping the execution of all other threads, thereby affecting the efficiency of the translation and execution of source program instructions.
为了解决上述技术问题,本申请是这样实现的:In order to solve the above technical problems, this application is implemented as follows:
第一方面,本申请实施例提供了一种二进制翻译方法,包括:In a first aspect, an embodiment of the present application provides a binary translation method, comprising:
从翻译器的缓存中获取每个基本块的翻译结果;Get the translation result of each basic block from the translator's cache;
在执行所述翻译结果中的指令时,若确定当前执行的指令为存在访存对齐异常的目标指令,则根据所述目标指令中包括的第一访存地址进行重新映射,生成第二访存地址,并将所述第一访存地址所对应的内存片区配置为不可访问;When executing an instruction in the translation result, if it is determined that the currently executed instruction is a target instruction with a memory access alignment exception, remapping is performed according to a first memory access address included in the target instruction to generate a second memory access address, and a memory slice corresponding to the first memory access address is configured as inaccessible;
针对所述第二访存地址执行访存操作,以访问所述第二访存地址的数据。A memory access operation is performed on the second memory access address to access data at the second memory access address.
第二方面,本申请实施例还提供了一种二进制翻译装置,包括:In a second aspect, an embodiment of the present application further provides a binary translation device, comprising:
第一获取模块,用于从翻译器的缓存中获取每个基本块的翻译结果;A first acquisition module is used to obtain the translation result of each basic block from the cache of the translator;
生成模块,用于在执行所述翻译结果中的指令时,若确定当前执行的指令为存在访存对齐异常的目标指令,则根据所述目标指令中包括的第一访存地址进行重新映射,生成第二访存地址,并将所述第一访存地址所对应的内存片区配置为不可访问;a generating module configured to, when executing an instruction in the translation result, if it is determined that the currently executed instruction is a target instruction with a memory access alignment exception, remap the instruction according to a first memory access address included in the target instruction to generate a second memory access address, and configure a memory slice corresponding to the first memory access address as inaccessible;
执行模块,用于针对所述第二访存地址执行访存操作,以访问所述第二访存地址的数据。An execution module is configured to execute a memory access operation on the second memory access address to access data at the second memory access address.
第三方面,本申请实施例还提供了一种电子设备,包括处理器,存储器及存储在所述存储器上并可在所述处理器上运行的程序或指令,所述程序或指令被所述处理器执行时实现如第一方面所述的方法。In a third aspect, an embodiment of the present application further provides an electronic device comprising a processor, a memory, and a program or instruction stored in the memory and executable on the processor, wherein the program or instruction, when executed by the processor, implements the method described in the first aspect.
第四方面,本申请实施例还提供了一种可读存储介质,当所述可读存储介质中的指令由电子设备的处理器执行时,使得电子设备能够执行如第一方面所述的方法。In a fourth aspect, an embodiment of the present application further provides a readable storage medium, which, when the instructions in the readable storage medium are executed by a processor of an electronic device, enables the electronic device to execute the method described in the first aspect.
综上,在本申请的二进制翻译方法中,可以通过根据存在访存对齐异常的第一访存地址来重新映射内存片区的访存地址,将非对齐访存转换为基于新的地址(第二访存地址)的访存操作,这样可以使得整个访存过程作为一个原子操作来执行,避免了被其他线程或进程干扰;并将所述第一访存地址所对应的内存片区配置为不可访问,本方案无需停止其他线程,而是通过重新映射内存的方式解决访存对齐问题,对于不涉及第一内存片区中数据操作的其他线程可以继续执行不受影响的任务,减少了不必要的线程阻塞和调度开销,从而提高了整体的执行效率,提高了整个系统的并发性能和源程序指令翻译执行的效率。In summary, in the binary translation method of the present application, the memory access address of the memory slice can be remapped according to the first memory access address with a memory access alignment exception, and the non-aligned memory access can be converted into a memory access operation based on the new address (the second memory access address). In this way, the entire memory access process can be executed as an atomic operation, avoiding interference from other threads or processes; and the memory slice corresponding to the first memory access address is configured as inaccessible. This solution does not need to stop other threads, but solves the memory alignment problem by remapping the memory. Other threads that are not involved in data operations in the first memory slice can continue to execute unaffected tasks, reducing unnecessary thread blocking and scheduling overhead, thereby improving the overall execution efficiency, the concurrency performance of the entire system, and the efficiency of source program instruction translation and execution.
附图说明BRIEF DESCRIPTION OF THE DRAWINGS
图1是本申请实施例提供的一种二进制翻译方法的流程图;FIG1 is a flowchart of a binary translation method provided in an embodiment of the present application;
图2是本申请实施例提供的一种二进制翻译方法的示意图;FIG2 is a schematic diagram of a binary translation method provided in an embodiment of the present application;
图3是本申请实施例提供的另一种二进制翻译方法的示意图;FIG3 is a schematic diagram of another binary translation method provided in an embodiment of the present application;
图4是本申请实施例提供的一种二进制翻译装置的框图;FIG4 is a block diagram of a binary translation device provided in an embodiment of the present application;
图5是根据一示例性实施例示出的一种电子设备的框图;FIG5 is a block diagram of an electronic device according to an exemplary embodiment;
图6是根据一示例性实施例示出的一种电子设备的框图。Fig. 6 is a block diagram of an electronic device according to an exemplary embodiment.
具体实施方式DETAILED DESCRIPTION
下面结合附图,通过具体的实施例及其应用场景对本申请实施例提供的二进制内存访问进行详细地说明。The binary memory access provided by the embodiment of the present application is described in detail below with reference to the accompanying drawings through specific embodiments and their application scenarios.
在介绍本申请提供的二进制翻译方法、装置、电子设备及可读存储介质之前,首先对本申请各个实施例所涉及的应用场景进行介绍。本申请可以应用于在目标架构平台上运行翻译结果的场景下,其中,该翻译结果为通过翻译器对源架构平台对应的源程序进行翻译后的目标程序。Before introducing the binary translation method, apparatus, electronic device, and readable storage medium provided by this application, we first introduce the application scenarios involved in each embodiment of this application. This application can be applied to the scenario of running the translation result on the target architecture platform, where the translation result is the target program after the source program corresponding to the source architecture platform is translated by the translator.
目前,不同指令集体系结构(ISA,Instruction Set Architecture)的硬件设计能支持的指令存在诸多差异,这导致基于一种ISA编译的可执行文件和其它平台不兼容。二进制翻译技术可以根据指令集架构平台的不同,对源架构平台的源程序进行翻译,以使源架构平台对应的源程序运行在另一种目标架构平台上。Currently, hardware designs based on different instruction set architectures (ISAs) support many different instructions, resulting in incompatibility between executable files compiled for one ISA and other platforms. Binary translation technology can translate source programs from one ISA platform to run on a target architecture, depending on the ISA platform.
源程序为基于源架构平台(客户机架构平台)开发的应用程序。也即,源程序是基于第一指令集架构开发的程序,因此,源程序可以在客户机架构平台上正常运行。目标架构平台(宿主机架构平台)是希望移植源程序的机器平台。源程序可以采用源二进制代码在客户机架构平台上运行。如果希望在目标架构平台上运行该源程序,则需要将该源程序对应的该源二进制代码翻译成目标二进制代码。该目标二进制代码为基于第二指令集架构的可执行代码(或宿主机代码),从而可以在目标架构平台上运行该目标二进制代码。也即,该源程序可以通过二进制翻译技术运行在该目标架构平台中的二进制翻译环境下。The source program is an application developed based on a source architecture platform (client architecture platform). That is, the source program is a program developed based on a first instruction set architecture, and therefore, the source program can run normally on the client architecture platform. The target architecture platform (host architecture platform) is the machine platform to which the source program is desired to be ported. The source program can be run on the client architecture platform using source binary code. If it is desired to run the source program on the target architecture platform, the source binary code corresponding to the source program needs to be translated into target binary code. The target binary code is an executable code (or host code) based on a second instruction set architecture, so that the target binary code can be run on the target architecture platform. That is, the source program can be run in a binary translation environment in the target architecture platform using binary translation technology.
其中,源程序中的原子指令是一类特殊的指令,其执行过程具有不可分割性(原子性)。在多线程或多处理器系统中,原子指令能够确保其操作在执行时不会被中断,从而避免竞争条件和数据一致性问题。原子指令支持的操作位数取决于指令的类型和处理器架构的特性,一些架构仅支持最高64位(8字节)的原子指令,而有些架构中最高支持128位(16字节)的原子指令,如x86-64中的cmpxchg16b指令,可以最高支持128位(16字节)的原子指令。Atomic instructions in source programs are a special type of instruction whose execution is indivisible (atomic). In multi-threaded or multi-processor systems, atomic instructions ensure that their operations are not interrupted during execution, thereby avoiding race conditions and data consistency issues. The number of operands supported by atomic instructions depends on the instruction type and the characteristics of the processor architecture. Some architectures only support atomic instructions of up to 64 bits (8 bytes), while others support atomic instructions of up to 128 bits (16 bytes). For example, the cmpxchg16b instruction in x86-64 can support atomic instructions of up to 128 bits (16 bytes).
由于宿主机平台与客户机平台的架构特性差异,宿主机平台在对客户机平台的指令进行翻译时,若指令对应的访存地址不满足宿主机平台支持的访问对齐状态的要求,在宿主平台中则会存在访存对齐异常,这样,就无法保证将客户平台的指令翻译到宿主平台后运行结果的一致性,进而影响源程序指令翻译的准确性。Due to the architectural differences between the host platform and the client platform, when the host platform translates the client platform's instructions, if the memory access address corresponding to the instruction does not meet the access alignment requirements supported by the host platform, a memory alignment exception will occur in the host platform. In this way, the consistency of the running results after translating the client platform's instructions to the host platform cannot be guaranteed, which in turn affects the accuracy of the source program instruction translation.
为了保证将客户平台的指令翻译到宿主平台后运行结果的一致性,通常会将一个非对齐访存操作拆分成多个对齐访存操作来执行,但是如果采用这种方式来模拟客户机中的原子指令,在多线程或多进程的并发环境中,由于拆分后的多个访存操作无法满足指令的原子性,在操作过程中会被其他线程或进程打断,会导致最终操作组合的执行结果不正确。In order to ensure the consistency of the running results after translating the instructions of the client platform to the host platform, an unaligned memory access operation is usually split into multiple aligned memory access operations for execution. However, if this method is used to simulate the atomic instructions in the client, in a multi-threaded or multi-process concurrent environment, the multiple memory access operations after the split cannot meet the atomicity of the instructions and will be interrupted by other threads or processes during the operation, resulting in incorrect execution results of the final operation combination.
相关技术中,在上述情况下,为了避免在操作过程中会被其他线程或进程打断,以及被其他线程或进程观察到中间状态或乱序结果,通常会采取在执行这条指令时使其它线程都停下来的方式,来保证指客户机中的原子指令在翻译后执行时的原子性,但是采用这种方式,无条件停止所有其他线程的执行,会影响源程序指令翻译执行的效率。In the related art, in the above situation, in order to avoid being interrupted by other threads or processes during the operation, and to avoid being observed by other threads or processes in the intermediate state or out-of-order results, it is usually adopted to stop other threads when executing this instruction to ensure the atomicity of the atomic instruction in the client when it is executed after translation. However, using this method to unconditionally stop the execution of all other threads will affect the efficiency of the translation and execution of the source program instructions.
为了解决上述问题,本申请提供一种二进制翻译方法、装置、电子设备及可读存储介质,可以通过根据存在访存对齐异常的第一访存地址来重新映射内存片区的访存地址,将非对齐访存转换为基于新的地址(第二访存地址)的访存操作,这样可以使得整个访存过程作为一个原子操作来执行,避免了被其他线程或进程干扰;并将该第一访存地址所对应的内存片区配置为不可访问,本方案无需停止其他线程,而是通过重新映射内存的方式解决访存对齐问题,对于不涉及第一内存片区中数据操作的其他线程可以继续执行不受影响的任务,减少了不必要的线程阻塞和调度开销,从而提高了整体的执行效率,提高了整个系统的并发性能和源程序指令翻译执行的效率。In order to solve the above problems, the present application provides a binary translation method, device, electronic device and readable storage medium, which can remap the memory access address of the memory slice according to the first memory access address with memory access alignment exception, and convert the non-aligned memory access into a memory access operation based on the new address (second memory access address). In this way, the entire memory access process can be executed as an atomic operation, avoiding interference from other threads or processes; and the memory slice corresponding to the first memory access address is configured as inaccessible. This solution does not need to stop other threads, but solves the memory alignment problem by remapping the memory. Other threads that do not involve data operations in the first memory slice can continue to execute unaffected tasks, reducing unnecessary thread blocking and scheduling overhead, thereby improving overall execution efficiency, improving the concurrency performance of the entire system and the efficiency of source program instruction translation and execution.
下面结合附图,通过具体的实施例及其应用场景对本申请实施例提供的方法进行详细地说明。The method provided in the embodiment of the present application is described in detail below through specific embodiments and their application scenarios in conjunction with the accompanying drawings.
图1是本实施例提供的一种二进制翻译方法的流程图,包括如下步骤:FIG1 is a flowchart of a binary translation method provided by this embodiment, comprising the following steps:
步骤101,从翻译器的缓存中获取每个基本块的翻译结果。Step 101: Obtain the translation result of each basic block from the cache of the translator.
在本步骤中,可以在对源程序包括的多个基本块执行的过程中,从翻译器的缓存中获取每个该基本块的翻译结果。In this step, during the execution of multiple basic blocks included in the source program, the translation result of each basic block can be obtained from the cache of the translator.
其中,源程序可以表征基于源架构平台(客户机架构平台)开发的应用程序。也即,源程序是基于第一指令集架构开发的程序且包括多个基本块,该基本块是指该源程序中顺序执行的一段指令序列,翻译器读取二进制文件后,以基本块为粒度对源程序进行翻译和执行。The source program may represent an application developed based on a source architecture platform (client architecture platform). Specifically, the source program is developed based on a first instruction set architecture and includes multiple basic blocks. A basic block is a sequence of instructions executed sequentially within the source program. After reading the binary file, the translator translates and executes the source program at the basic block level.
首先,在需要将基于源架构平台开发的源程序运行在目标架构平台时,可以针对该源程序中包括的每一个该基本块,根据实际执行到该基本块的指令,实时把该基本块中的源架构指令翻译成目标架构指令。First, when it is necessary to run the source program developed based on the source architecture platform on the target architecture platform, the source architecture instructions in each basic block included in the source program can be translated into target architecture instructions in real time based on the instructions actually executed on the basic block.
示例的,该源架构平台可以表示为x86架构平台,该目标架构平台可以表示为另一种架构平台,该二进制程序可以表示为x86程序,在将基于x86架构平台开发的x86程序运行于另一种架构平台上时,可以针对x86程序中的一个基本块,对于该基本块中的指令进行翻译。For example, the source architecture platform can be represented as an x86 architecture platform, the target architecture platform can be represented as another architecture platform, and the binary program can be represented as an x86 program. When an x86 program developed based on the x86 architecture platform is run on another architecture platform, instructions in a basic block in the x86 program can be translated.
例如,一种x86程序中包括的基本块指令可以表示如下:For example, a basic block instruction included in an x86 program can be represented as follows:
MOV EAX,5;将立即数5赋值给EAX寄存器;MOV EAX,5; assign the immediate value 5 to the EAX register;
MOV EBX,3;将立即数3赋值给EBX寄存器;MOV EBX,3; assign the immediate value 3 to the EBX register;
ADD EAX,EBX;将EAX和EBX的值相加,结果存于EAX;ADD EAX,EBX; Add the values of EAX and EBX and store the result in EAX;
MOV DWORD PTR [0x1000],EAX;将EAX的值存储到内存地址[0x1000]处。MOV DWORD PTR [0x1000],EAX; stores the value of EAX to the memory address [0x1000].
由于在另一种架构平台的指令集中,寄存器命名和指令格式与x86有所不同,可以将另一种架构平台的通用寄存器设为a0、a1等,在将该基本块指令翻译为另一种架构平台上可运行的指令时,该基本块对应的翻译结果可以表示如下:Since the register naming and instruction format of the instruction set of another architecture platform are different from those of x86, the general registers of the other architecture platform can be set to a0, a1, etc. When translating the basic block instructions into instructions that can be executed on the other architecture platform, the translation results corresponding to the basic block can be expressed as follows:
li a0,5;将立即数5加载到a0寄存器;li a0,5; load the immediate value 5 into the a0 register;
li a1,3;将立即数3加载到a1寄存器;li a1,3; load the immediate value 3 into the a1 register;
add a0,a0,a1;将a0和a1的值相加,结果存于a0;add a0,a0,a1; add the values of a0 and a1 and store the result in a0;
sd a0,0x1000;将a0的值存储到内存地址0x1000处。sd a0,0x1000; stores the value of a0 to memory address 0x1000.
可以将上述翻译后的指令序列作为x86程序中包括的基本块指令的翻译结果,并将该翻译后的指令序列存储至缓存,在对该基本块进行执行的情况下,可以从缓存中获取该基本块的翻译结果。The translated instruction sequence can be used as the translation result of the basic block instructions included in the x86 program, and the translated instruction sequence can be stored in the cache. When the basic block is executed, the translation result of the basic block can be obtained from the cache.
步骤102,在执行该翻译结果中的指令时,若确定当前执行的指令为存在访存对齐异常的目标指令,则根据该目标指令中包括的第一访存地址进行重新映射,生成第二访存地址,并将该第一访存地址所对应的内存片区配置为不可访问。Step 102: When executing the instruction in the translation result, if it is determined that the currently executed instruction is a target instruction with a memory access alignment exception, remapping is performed according to the first memory access address included in the target instruction to generate a second memory access address, and the memory slice corresponding to the first memory access address is configured as inaccessible.
其中,该第二访存地址是对根据该第一访存地址确定出的内存片区进行重新映射后得到的地址。The second memory access address is an address obtained by remapping the memory area determined according to the first memory access address.
考虑到不同架构中对其所支持的指令的访存地址对齐也有不同的规定,在x86架构中指令对内存访问的地址对齐要求相对宽松,例如,x86架构允许非对齐的内存访问,尽管非对齐访问会导致性能下降,尤其是在涉及跨越缓存行或内存页边界时,但对于使用LOCK前缀的原子指令,如LOCK XADD(用于在多处理器环境下以原子方式完成交换并相加的操作)或LOCK CMPXCHG(同于在多处理器环境下以原子方式完成比较并交换的操作),即使目标地址未对齐,x86架构下也能保证操作的原子性。而在经过二进制翻译后的另一种架构中,原子指令对内存访问地址的对齐是有要求的,对原子操作的内存地址要求对齐到数据的自然边界,这意味着32位数据需要4字节对齐,64位数据需要8字节对齐。如果原子指令的目标地址未对齐,则会引发未对齐访问异常,导致程序崩溃或不确定的行为。Given that different architectures have different memory address alignment requirements for their supported instructions, the x86 architecture has relatively relaxed memory address alignment requirements. For example, the x86 architecture allows unaligned memory accesses. Although unaligned accesses can result in performance degradation, especially when crossing cache line or memory page boundaries, atomic instructions using the LOCK prefix, such as LOCK XADD (for atomic swap-add operations in multiprocessor environments) or LOCK CMPXCHG (for atomic compare-and-swap operations in multiprocessor environments), guarantee atomicity even when the target address is unaligned. On the other hand, atomic instructions on other architectures, after binary translation, have memory address alignment requirements. The memory address of an atomic operation must be aligned to a natural data boundary, meaning 4-byte alignment for 32-bit data and 8-byte alignment for 64-bit data. If the target address of an atomic instruction is unaligned, an unaligned access exception is thrown, causing a program crash or undefined behavior.
另外,原子指令支持的操作位数取决于指令的类型和处理器架构的特性,一些架构仅支持最高64位(8字节)的原子指令,如RISC-V架构(精简指令集架构),而有些架构中最高支持128位(16字节)的原子指令,如x86-64中的cmpxchg16b指令(在x86-64架构中用于进行16字节(128位)比较并交换操作的指令),可以最高支持128位(16字节)的原子指令。在将基于x86-64的指令翻译至RISC-V架构上运行的情况下,虽然在有些情况下能够保证访存地址对齐,但仍存在翻译后RISC-V架构下的指令无法保证指令的原子性的情况。In addition, the number of operands supported by atomic instructions depends on the type of instruction and the characteristics of the processor architecture. Some architectures only support atomic instructions of up to 64 bits (8 bytes), such as the RISC-V architecture (reduced instruction set architecture), while some architectures support atomic instructions of up to 128 bits (16 bytes), such as the cmpxchg16b instruction in x86-64 (an instruction used to perform 16-byte (128-bit) compare and exchange operations in the x86-64 architecture), which can support atomic instructions of up to 128 bits (16 bytes). When translating x86-64-based instructions to the RISC-V architecture for execution, although memory address alignment can be guaranteed in some cases, there are still cases where the atomicity of instructions under the translated RISC-V architecture cannot be guaranteed.
例如,在将基于x86-64的指令翻译至RISC-V架构上运行的情况下,可以在定义结构体时,让每个数据成员的起始地址是其自身大小的整数倍。比如一个结构体包含一个4字节的整数和一个8字节的双精度浮点数,将4字节整数放在前面,且保证其地址是4的倍数,8字节双精度浮点数放在后面且地址是8的倍数。这样在进行访存操作时,就能自然满足RISC-V的对齐要求。For example, when translating x86-64-based instructions to the RISC-V architecture, you can define a structure so that the starting address of each data member is an integer multiple of its size. For example, if a structure contains a 4-byte integer and an 8-byte double-precision floating-point number, place the 4-byte integer first, ensuring its address is a multiple of 4, and the 8-byte double-precision floating-point number at the end, ensuring its address is a multiple of 8. This way, memory accesses naturally meet RISC-V's alignment requirements.
综上,在执行该源程序中基本块的指令序列时,在宿主机所能支持原子指令的最大位宽小于待执行的客户机中源程序的指令所包括指令位宽的情况下,可以认为待执行的目标指令存在访存对齐异常;或者在宿主机架构下的原子指令的访存地址对齐规定比客户机架构下的原子指令的访存地址对齐规定严格时,同样可以认为待执行的目标指令存在访存对齐异常。In summary, when executing the instruction sequence of the basic block in the source program, when the maximum bit width of the atomic instruction that the host machine can support is smaller than the instruction bit width included in the instructions of the source program in the client to be executed, it can be considered that there is a memory access alignment exception in the target instruction to be executed; or when the memory access address alignment regulations of the atomic instructions under the host machine architecture are stricter than the memory access address alignment regulations of the atomic instructions under the client architecture, it can also be considered that there is a memory access alignment exception in the target instruction to be executed.
示例的,在一种可能的实现方式中,为了保证原子指令在执行时的原子性,可以将非对齐访存操作拆分成多个对齐访存操作来执行,例如,在32位系统中,要从内存地址0x1001开始读取一个32位(4字节)的整数,由于起始地址不是4的倍数,这是一个非对齐访存操作,在此情况下,可以计算非对齐地址相对于4字节对齐边界的偏移量,如果偏移量大于0,就可以将数据拆分成两部分进行读取,一部分是从非对齐地址开始到下一个对齐边界之前的字节,另一部分是下一个对齐块中需要的字节,如果地址已经对齐,直接读取4字节数据。For example, in one possible implementation, in order to ensure the atomicity of atomic instructions during execution, unaligned memory access operations can be split into multiple aligned memory access operations for execution. For example, in a 32-bit system, to read a 32-bit (4-byte) integer starting from memory address 0x1001, since the starting address is not a multiple of 4, this is an unaligned memory access operation. In this case, the offset of the unaligned address relative to the 4-byte alignment boundary can be calculated. If the offset is greater than 0, the data can be split into two parts for reading. One part is the bytes from the unaligned address to the next alignment boundary, and the other part is the bytes required in the next aligned block. If the address is already aligned, the 4-byte data is read directly.
或者,在另一种可能的实现方式中,为了保证原子指令在执行时的原子性,在宿主机所能支持原子指令的最大位宽小于待执行的客户机中源程序的指令所包括指令位宽时,通过自旋锁结合多次原子操作的方式来执行,在以RISC-V模拟x86-64的128位原子交换或累加操作为例的情况下,利用宿主机支持的原子指令(如RISC-V的64位原子指令),通过循环尝试的方式,在每次尝试中对目标数据的不同部分进行原子操作。在操作过程中,使用自旋锁来保证同一时间只有一个线程能对目标数据进行操作。例如在实现128位数据原子交换时,先读取数据的高64位和低64位,然后尝试用原子比较交换指令分别对高64位和低64位进行交换操作,如果其中任何一次操作失败,就重新开始整个过程,从而通过多次尝试和同步机制,确保最终操作的原子性。Alternatively, in another possible implementation, to ensure the atomicity of atomic instructions during execution, when the maximum bit width of atomic instructions supported by the host machine is smaller than the instruction bit width of the source program to be executed on the client machine, execution is performed using a spin lock combined with multiple atomic operations. For example, in the case of RISC-V emulating x86-64's 128-bit atomic swap or accumulation operations, atomic instructions supported by the host machine (such as RISC-V's 64-bit atomic instructions) are used in a loop, performing atomic operations on different parts of the target data in each attempt. During the operation, a spin lock is used to ensure that only one thread can operate on the target data at a time. For example, when implementing a 128-bit atomic swap of data, the upper and lower 64 bits of the data are first read, and then atomic compare-and-swap instructions are used to swap the upper and lower 64 bits. If any of these operations fail, the entire process is restarted. Thus, through multiple attempts and synchronization mechanisms, the atomicity of the final operation is ensured.
但是在多线程或多进程的并发环境中,由于拆分后的多个访存操作不是原子操作,在访存操作过程中会被其他正在运行的线程或进程打断,会导致最终操作组合的执行结果不正确。例如,假设在32位系统中,从地址0x1001读取一个32位整数,我们将其拆分成两部分读取:从0x1001到0x1003的部分和从0x1004开始的部分。如果在访问读取这两部分的过程中,有其他线程或进程通过数据存储的形式修改了相关内存区域的数据,就导致最终读取到的数据不是一个连贯的、正确的32位整数。若线程A正在进行非对齐访存拆分操作,先读取了0x1001-0x1003的数据,此时线程B修改了0x1004处的数据,然后线程A再读取0x1004开始的数据,这样线程A组合得到的32位整数就是错误的,导致了访存对齐异常。However, in a concurrent multi-threaded or multi-process environment, since the multiple memory access operations are not atomic, they can be interrupted by other running threads or processes during the access process, resulting in incorrect execution of the combined operations. For example, in a 32-bit system, a 32-bit integer is read from address 0x1001. This is split into two parts: the part from 0x1001 to 0x1003 and the part starting at 0x1004. If another thread or process modifies the data in the corresponding memory area during the access, the final read data will not be a coherent and correct 32-bit integer. If thread A performs an unaligned memory access split operation, first reading the data at 0x1001-0x1003, thread B then modifies the data at 0x1004, and thread A then reads the data starting at 0x1004, the 32-bit integer obtained by thread A will be incorrect, resulting in a memory access alignment exception.
本申请中,在确定当前执行的指令为存在访存对齐异常的目标指令时,可以根据该目标指令中包括的第一访存地址进行重新映射,生成对应的第二访存地址,并将该第一访存地址所对应的内存片区配置为不可访问。In the present application, when it is determined that the currently executed instruction is a target instruction with a memory access alignment exception, remapping can be performed based on the first memory access address included in the target instruction to generate a corresponding second memory access address, and the memory slice corresponding to the first memory access address can be configured as inaccessible.
在本实施例中,首先可以确定当前执行的指令为存在访存对齐异常的目标指令。In this embodiment, it may be first determined that the currently executed instruction is a target instruction with a memory access alignment exception.
可选的,在指令执行过程中,系统会对每条涉及内存访问的指令进行检查。检查的方式通常是计算指令的访存地址对相应数据类型对齐字节数取模,通过比对指令的访存地址与指令涉及的数据类型来判断是否满足对齐要求,如果结果不为零,就说明该访存地址未满足对齐要求,此时这条指令就是存在访存对齐异常的目标指令。例如,对于一个32位整数的访问指令,若其访存地址对4取模不为0,表示该地址不是4的整数倍,即该地址不是按照4字节对齐的方式进行访问的,即可判定该指令存在访存对齐异常。Optionally, during the execution of instructions, the system will check each instruction involving memory access. The checking method is usually to calculate the memory access address of the instruction modulo the number of alignment bytes of the corresponding data type, and to determine whether the alignment requirements are met by comparing the memory access address of the instruction with the data type involved in the instruction. If the result is not zero, it means that the memory access address does not meet the alignment requirements. At this time, this instruction is the target instruction with a memory access alignment exception. For example, for a 32-bit integer access instruction, if its memory access address modulo 4 is not 0, it means that the address is not an integer multiple of 4, that is, the address is not accessed in a 4-byte aligned manner, and it can be determined that the instruction has a memory access alignment exception.
可选的,可以根据该目标指令中包括的第一访存地址的地址类型,获取该目标指令中包括的初始访存地址;然后对该初始访存地址所对应的内存片区进行重新映射,得到映射后的该第二访存地址。Optionally, the initial memory access address included in the target instruction can be obtained based on the address type of the first memory access address included in the target instruction; then the memory slice corresponding to the initial memory access address is remapped to obtain the mapped second memory access address.
在一种可能的实现方式中,该目标指令中包括的第一访存地址是该目标指令在翻译前的基本块中对应指令中所包括的初始访存地址,其中,该初始访存地址为非对齐访存地址类型的访存地址;然后可以对该初始访存地址按照对齐的方式直接进行翻译,得到包括该初始访存地址的目标指令,在此情况下,该目标指令中第一访存地址与该初始访存地址一致。In one possible implementation, the first memory access address included in the target instruction is the initial memory access address included in the corresponding instruction in the basic block of the target instruction before translation, wherein the initial memory access address is an access address of the non-aligned memory access address type; then the initial memory access address can be directly translated in an aligned manner to obtain a target instruction including the initial memory access address. In this case, the first memory access address in the target instruction is consistent with the initial memory access address.
例如,在将基于x86架构平台开发的x86程序运行于RISC-V架构平台上时,基于x86-64的指令序列可以表示如下:For example, when running an x86 program developed based on the x86 architecture platform on the RISC-V architecture platform, the instruction sequence based on x86-64 can be expressed as follows:
mov eax,0x1003;假设数据地址存放在寄存器eax中;mov eax,0x1003;Assume that the data address is stored in register eax;
mov ebx,[eax];从非对齐地址读取32位数据到寄存器ebx;mov ebx,[eax]; read 32-bit data from unaligned address to register ebx;
inc ebx;对数据进行加1操作;inc ebx; add 1 to the data;
mov [eax],ebx;将修改后的数据写回非对齐地址;mov [eax],ebx; write the modified data back to the unaligned address;
其中,0x1003就是初始访存地址,由于32位数据的理想对齐地址是4的倍数,因此0x1003是非对齐的。然后可以对初始访存地址0x1003按照对齐的方式直接进行翻译,得到RISC-V架构下的目标指令序列,表示如下:Among them, 0x1003 is the initial memory access address. Since the ideal alignment address of 32-bit data is a multiple of 4, 0x1003 is unaligned. Then the initial memory access address 0x1003 can be directly translated according to the alignment method to obtain the target instruction sequence under the RISC-V architecture, which is expressed as follows:
li a0,0x1003;将地址0x1003存入寄存器a0;li a0,0x1003;Store address 0x1003 into register a0;
lw t0,0(a0);从对齐地址0x1000读取32位数据到寄存器t0;lw t0,0(a0); read 32-bit data from aligned address 0x1000 to register t0;
;通过逻辑运算提取出对应0x1003处的数据;;Extract the data corresponding to 0x1003 through logical operation;
andi t1,t0,0x000000FF;假设t1用于存储最终需要的数据;andi t1,t0,0x000000FF; Assume that t1 is used to store the data that is ultimately needed;
addi t1,t1,1;对数据进行加1操作;addi t1,t1,1; add 1 to the data;
;构建完整的32位数据以便写回;;Build the complete 32-bit data for writing back;
slli t2,t1,24;slli t2,t1,24;
or t0,t0,t2;假设t2是一个临时寄存器;or t0,t0,t2; Assume t2 is a temporary register;
sw t0,0(a0);将修改后的数据写回地址0x1003;sw t0,0(a0); write the modified data back to address 0x1003;
由上述指令序列可知,第一访存地址(即0x1003)与初始访存地址一致。可以通过对从对齐地址0x1000读取的数据进行逻辑运算来模拟对非对齐地址0x1003的操作。From the above instruction sequence, we can see that the first memory access address (i.e., 0x1003) is consistent with the initial memory access address. We can simulate the operation on the unaligned address 0x1003 by performing logical operations on the data read from the aligned address 0x1000.
在另一种可能的实现方式中,该目标指令中包括的第一访存地址是预先设置的预设非对齐访存地址,这是因为该目标指令在翻译前的基本块中对应的指令,其包括的初始访存地址的指令位宽大于宿主机架构所支持的最大位宽,虽然在有些情况下能够保证访存地址对齐,但是存在翻译后宿主机指令无法保证指令的原子性的情况,因此在这种情况下,可以将该预设非对齐访存地址存储于预设寄存器,并在当前翻译的指令翻译后得到的对应指令位置处,写入包括预设非对齐访存地址的指令,这样可以利用预设非对齐访存地址能够触发执行停止信号的特性,来使得后续能够采用本申请的步骤来保证翻译后指令的原子性。In another possible implementation, the first memory access address included in the target instruction is a pre-set preset unaligned memory access address. This is because the instruction corresponding to the target instruction in the basic block before translation includes an initial memory access address whose instruction bit width is greater than the maximum bit width supported by the host architecture. Although the memory access address alignment can be guaranteed in some cases, there are cases where the atomicity of the host instruction cannot be guaranteed after translation. Therefore, in this case, the preset unaligned memory access address can be stored in a preset register, and the instruction including the preset unaligned memory access address can be written to the corresponding instruction position obtained after the current translated instruction is translated. In this way, the characteristic that the preset unaligned memory access address can trigger the execution stop signal can be utilized to enable the subsequent steps of this application to ensure the atomicity of the translated instruction.
例如,在将基于x86-64的指令翻译至RISC-V架构上运行的情况下,可以在定义结构体时,让每个数据成员的起始地址是其自身大小的整数倍。比如一个结构体包含一个4字节的整数和一个8字节的双精度浮点数,将4字节整数放在前面,且保证其地址是4的倍数,8字节双精度浮点数放在后面且地址是8的倍数。这样在进行访存操作时,就能自然满足RISC-V的对齐要求。For example, when translating x86-64-based instructions to the RISC-V architecture, you can define a structure so that the starting address of each data member is an integer multiple of its size. For example, if a structure contains a 4-byte integer and an 8-byte double-precision floating-point number, place the 4-byte integer first, ensuring its address is a multiple of 4, and the 8-byte double-precision floating-point number at the end, ensuring its address is a multiple of 8. This way, memory accesses naturally meet RISC-V's alignment requirements.
综上,在通过上述方式的情况下,该目标指令中包括的第一访存地址可以对应的不同的地址类型,具体的,可以通过确定该第一访存地址是否满足预设指令模板,来确定该目标指令中包括的第一访存地址的地址类型,该预设指令模板中包括不同的指令地址类型,在该第一访存地址满足预设指令模板中的第一类型的情况下,该第一访存地址的地址类型可以为预设非对齐访存地址,该预设非对齐访存地址首先是非对齐的指令,在此基础上,该预设非对齐访存地址还是研发人员预先设置的固定的一串编码,例如,可以预先将非对齐访存地址设置为0x1003,也就是说,在确定指令中包括的访存地址的具体值为0x1003的情况下,就可以确定该第一访存地址为预设非对齐访存地址。In summary, in the case of the above method, the first memory access address included in the target instruction can correspond to different address types. Specifically, the address type of the first memory access address included in the target instruction can be determined by determining whether the first memory access address satisfies the preset instruction template. The preset instruction template includes different instruction address types. When the first memory access address satisfies the first type in the preset instruction template, the address type of the first memory access address can be a preset unaligned memory access address. The preset unaligned memory access address is first an unaligned instruction. On this basis, the preset unaligned memory access address is also a fixed string of codes pre-set by the R&D personnel. For example, the unaligned memory access address can be set to 0x1003 in advance. That is to say, when the specific value of the memory access address included in the instruction is determined to be 0x1003, it can be determined that the first memory access address is the preset unaligned memory access address.
相应的,在该第一访存地址满足预设指令模板的中第二类型的情况下,该第一访存地址的地址类型可以为非对齐访存地址,该非对齐访存地址指的是在进行内存访问操作时,访问的起始地址不是所访问数据类型大小的整数倍。例如,在一个要求4字节对齐的系统中,如果要访问一个4字节的整数,正常情况下其起始地址应该是4的倍数(如0、4、8等),若起始地址是1、2或3等非4的倍数的地址,则属于非对齐访存地址。Accordingly, if the first memory access address satisfies the second type of the preset instruction template, the address type of the first memory access address may be an unaligned memory access address. An unaligned memory access address refers to a memory access operation in which the starting address of the access is not an integer multiple of the size of the accessed data type. For example, in a system requiring 4-byte alignment, if a 4-byte integer is to be accessed, its starting address should normally be a multiple of 4 (such as 0, 4, 8, etc.). If the starting address is 1, 2, or 3, which is not a multiple of 4, it is considered an unaligned memory access address.
在该目标指令中包括的第一访存地址的地址类型为预设非对齐访存地址的情况下,可以首先获取寄存器中存储的访存地址,然后将该寄存器中存储的访存地址作为该目标指令中包括的初始访存地址。其中,该目标指令为在当前翻译的指令翻译后得到的对应指令位置处,写入的包括预设非对齐访存地址的停止指令,该访存地址为该当前翻译的指令中所包括的访存地址。When the address type of the first memory access address included in the target instruction is a preset unaligned memory access address, the memory access address stored in the register may be first obtained, and then the memory access address stored in the register may be used as the initial memory access address included in the target instruction. The target instruction is a stop instruction including the preset unaligned memory access address written at a corresponding instruction location obtained after translating the currently translated instruction, and the memory access address is the memory address included in the currently translated instruction.
以及在确定该目标指令中包括的该第一访存地址的地址类型为非对齐访存地址的情况下,直接将该第一访存地址作为该目标指令中包括的初始访存地址。And when it is determined that the address type of the first memory access address included in the target instruction is a non-aligned memory access address, the first memory access address is directly used as the initial memory access address included in the target instruction.
然后可以针对预设非对齐访存地址和非对齐访存地址两种第一访存地址的地址类型,均对该初始访存地址所对应的内存片区进行重新映射,得到映射后的该第二访存地址。Then, for the two address types of the first memory access address, namely the preset non-aligned memory access address and the non-aligned memory access address, the memory slice corresponding to the initial memory access address may be remapped to obtain the mapped second memory access address.
可选的,首先可以获取该初始访存地址所对应的内存片区的内存容量。Optionally, the memory capacity of the memory slice corresponding to the initial memory access address may be obtained first.
示例的,可以根据该初始访存地址所对应的内存片区要访问的数据大小,确定需要分配的空闲内存片区的大小。For example, the size of the free memory area to be allocated may be determined according to the size of data to be accessed by the memory area corresponding to the initial memory access address.
例如,在一个系统中,页面大小为4KB(即2^12字节),假设进程当前的初始访存地址为0x00001200,且需要访问从该地址开始的连续3KB数据。由于页面大小为4KB,3KB数据未跨越页面边界,仍然在虚拟页号为1的这一页内,因此确定需要分配的空闲内存片区的大小为一个页面。For example, in a system with a 4KB page size (2^12 bytes), assume that the process's current initial memory access address is 0x00001200 and needs to access 3KB of continuous data starting at this address. Since the page size is 4KB, the 3KB of data does not cross the page boundary and remains within the page with virtual page number 1. Therefore, the size of the free memory segment to be allocated is determined to be one page.
然后可以在该缓存中查找包括该内存容量在内的空闲内存片区。The cache can then be searched for a free memory slice that includes the memory capacity.
可选的,可以使用操作系统提供的系统调用函数(如在Linux系统中用于重新映射内存区域的系统调用的mremap函数)来查找包括该内存容量的空闲内存片区。Alternatively, a system call function provided by the operating system (such as the mremap function of the system call for remapping a memory area in a Linux system) may be used to search for a free memory area that includes the memory capacity.
示例的,首先可以获取该初始访存地址所对应的内存片区的第一起始地址,以及该初始访存地址所对应的内存片区的内存大小,然后可以确定重新映射行为的标志位,其中,该标志位用于控制重新映射函数mremap的行为,常见的标志有包括MREMAP_MAYMOVE标志以及MREMAP_FIXED标志,其中,MREMAP_MAYMOVE标志用于允许操作系统将映射区域移动到新的地址。如果不指定该标志,操作系统就无法在原地址处扩展映射区域,则mremap重新映射会失败;以及MREMAP_FIXED标志用于表示新的映射地址将由额外的参数new_address(地址参数)指定。For example, first, the first starting address of the memory slice corresponding to the initial memory access address and the memory size of the memory slice corresponding to the initial memory access address can be obtained, and then the flag of the remapping behavior can be determined, wherein the flag is used to control the behavior of the remapping function mremap. Common flags include the MREMAP_MAYMOVE flag and the MREMAP_FIXED flag. The MREMAP_MAYMOVE flag is used to allow the operating system to move the mapping area to a new address. If this flag is not specified, the operating system cannot expand the mapping area at the original address, and the mremap remapping will fail; and the MREMAP_FIXED flag is used to indicate that the new mapping address will be specified by the additional parameter new_address (address parameter).
具体的,若在使用mremap函数时设置MREMAP_MAYMOVE标志位,则可以将映射区域(即初始访存地址所对应的内存片区)移动到空闲内存片区,以及若在使用mremap函数时设置MREMAP_FIXED标志位,则可以由额外的参数(new_address)来指定新的映射地址(即空闲内存片区的第二起始地址)。指定新的映射地址时可以调用mremap函数,用于根据初始访存地址所对应的内存片区的内存大小以及该初始访存地址所对应的内存片区的第一起始地址,将初始访存地址所对应的内存片区移动至空闲内存片区。Specifically, if the MREMAP_MAYMOVE flag is set when using the mremap function, the mapping area (i.e., the memory slice corresponding to the initial memory access address) can be moved to the free memory slice, and if the MREMAP_FIXED flag is set when using the mremap function, the new mapping address (i.e., the second starting address of the free memory slice) can be specified by an additional parameter (new_address). When specifying a new mapping address, the mremap function can be called to move the memory slice corresponding to the initial memory access address to the free memory slice based on the memory size of the memory slice corresponding to the initial memory access address and the first starting address of the memory slice corresponding to the initial memory access address.
其中,在一种可能的实现方式中,在指定MREMAP_FIXED标志的情况下,可以由额外的参数(new_address)来指定新的映射地址(即空闲内存片区的第二起始地址)。In one possible implementation, when the MREMAP_FIXED flag is specified, an additional parameter (new_address) may be used to specify a new mapping address (ie, the second starting address of the free memory area).
在另一种可能的实现方式中,在指定MREMAP_MAYMOVE标志的情况下,操作系统会尝试在其他位置找到足够大的连续空闲内存块,并将原映射区域的数据复制到新的位置,此时新的起始地址会是操作系统找到的空闲内存片区的第二起始地址。In another possible implementation, when the MREMAP_MAYMOVE flag is specified, the operating system will try to find a sufficiently large continuous free memory block at another location and copy the data of the original mapping area to the new location. At this time, the new starting address will be the second starting address of the free memory area found by the operating system.
在一些实施例中,该空闲内存片区的访存地址是指在将该初始访存地址所对应的内存片区重新映射至对应的内存片区后,为了能够正确访问重新映射后的内存片区中的数据而生成的地址。In some embodiments, the memory access address of the free memory slice refers to an address generated after the memory slice corresponding to the initial memory access address is remapped to the corresponding memory slice in order to correctly access data in the remapped memory slice.
在本步骤中,在生成该空闲内存片区的访存地址之前,需要确定空闲内存片区已成功映射,具体的,可以通过确定mremap操作已成功完成,且得到了有效的空闲内存片区的第二起始地址的方式确定空闲内存片区已成功映射。In this step, before generating the memory access address of the free memory slice, it is necessary to determine that the free memory slice has been successfully mapped. Specifically, it can be determined that the free memory slice has been successfully mapped by determining that the mremap operation has been successfully completed and the second starting address of the valid free memory slice has been obtained.
示例的,可以通过检查mremap函数的返回值的方式,来确定映射操作是否已成功完成,mremap函数调用成功时会返回重新映射后的内存区域的起始地址(即空闲内存片区的第二起始地址),失败时会返回MAP_FAILED(通常被定义为(void *)-1)。因此,可以检查返回值以确定操作是否成功。如果返回的空闲内存片区的第二起始地址不等于MAP_FAILED,则表示mremap映射操作成功,则对该初始访存地址所对应的内存片区进行重新映射,并已成功映射至空闲内存片区,此时可以继续使用新的映射区域空闲内存片区,否则如果返回的空闲内存片区的第二起始地址等于MAP_FAILED,表示mremap映射操作失败,表示对该初始访存地址所对应的内存片区进行的重新映射的操作失败。For example, you can check the return value of the mremap function to determine whether the mapping operation has been completed successfully. When the mremap function call is successful, it returns the starting address of the remapped memory area (that is, the second starting address of the free memory segment). When it fails, it returns MAP_FAILED (usually defined as (void *)-1). Therefore, you can check the return value to determine whether the operation is successful. If the second starting address of the returned free memory segment is not equal to MAP_FAILED, it means that the mremap mapping operation is successful, and the memory segment corresponding to the initial memory access address is remapped and successfully mapped to the free memory segment. At this time, you can continue to use the new mapping area free memory segment. Otherwise, if the second starting address of the returned free memory segment is equal to MAP_FAILED, it means that the mremap mapping operation failed, indicating that the remapping operation of the memory segment corresponding to the initial memory access address failed.
在确定空闲内存片区已成功映射的情况下,可以生成该空闲内存片区的访存地址。When it is determined that the free memory area has been successfully mapped, a memory access address of the free memory area may be generated.
示例的,首先可以确定要访问的访存地址相对于第一起始地址的偏移量,其中,该第一起始地址为初始访存地址所对应的内存片区的起始地址;然后可以将空闲内存片区的第二起始地址与偏移量相加,即可得到该空闲内存片区的访存地址,该空闲内存片区的访存地址即为第二访存地址。For example, first, the offset of the memory address to be accessed relative to the first starting address can be determined, where the first starting address is the starting address of the memory slice corresponding to the initial memory address; then the second starting address of the free memory slice can be added to the offset to obtain the memory address of the free memory slice, and the memory address of the free memory slice is the second memory address.
在一些实施例中,在对该初始访存地址所对应的内存片区进行重新映射的情况下,可以将该初始访存地址所对应的内存片区的第一起始地址映射为该空闲内存片区的第二起始地址。In some embodiments, when the memory slice corresponding to the initial memory access address is remapped, the first starting address of the memory slice corresponding to the initial memory access address may be mapped to the second starting address of the free memory slice.
例如,在一个系统中,页面大小为4KB(即2^12字节),一个进程拥有以下初始的虚拟地址到物理地址的映射关系:For example, on a system with a page size of 4KB (or 2^12 bytes), a process has the following initial virtual address to physical address mapping:
假设进程当前的初始访存地址为0x00001200,且需要访问从该地址开始的连续3KB数据。由于页面大小为4KB,3KB数据未跨越页面边界,仍然在虚拟页号为1的这一页内;现在,假设系统需要将这部分数据的虚拟地址区间进行调整,比如将其映射到新的虚拟地址区间0x00008000-0x00008FFF。Assume that the process's current initial memory access address is 0x00001200, and it needs to access 3KB of continuous data starting at this address. Since the page size is 4KB, the 3KB of data does not cross the page boundary and remains within the page with virtual page number 1. Now, suppose that the system needs to adjust the virtual address range of this data, for example, mapping it to the new virtual address range 0x00008000-0x00008FFF.
首先可以通过查找映射关系可知,当前虚拟页号1对应的物理页框号为5,然后在页表中新增一个映射项,将虚拟页号对应新的虚拟地址区间(这里新虚拟页号可设为8,对应虚拟地址0x00008000-0x00008FFF),并将其映射到原来的物理页框号5;同时,由于不再需要原来的虚拟地址区间0x00001000-0x00001FFF对物理页框号5的映射,可将该映射项从页表中删除(或者标记为无效)。First, by looking up the mapping relationship, we can know that the physical page frame number corresponding to the current virtual page number 1 is 5. Then, a new mapping entry is added to the page table, and the virtual page number corresponds to the new virtual address range (here the new virtual page number can be set to 8, corresponding to the virtual address 0x00008000-0x00008FFF), and it is mapped to the original physical page frame number 5; at the same time, since the mapping of the original virtual address range 0x00001000-0x00001FFF to the physical page frame number 5 is no longer needed, the mapping entry can be deleted from the page table (or marked as invalid).
这样当进程后续访问新虚拟地址区间0x00008000-0x00008FFF内的地址时,内存管理单元(MMU)会根据更新后的页表,将其正确映射到物理地址0x00005000-0x00005FFF,实现了在不更换物理页的情况下,基于初始访存地址对应数据大小(未跨页)对虚拟地址的重映射。In this way, when the process subsequently accesses an address in the new virtual address range 0x00008000-0x00008FFF, the memory management unit (MMU) will correctly map it to the physical address 0x00005000-0x00005FFF based on the updated page table, thereby achieving remapping of the virtual address based on the data size corresponding to the initial memory access address (without crossing pages) without changing the physical page.
通过上述操作,系统可以根据初始访存地址对应的数据大小,在不更换物理页的情况下,完成了虚拟地址的重映射,使得进程能够通过新的虚拟地址区间访问到原来物理页上的数据。Through the above operations, the system can complete the remapping of the virtual address according to the data size corresponding to the initial memory access address without changing the physical page, so that the process can access the data on the original physical page through the new virtual address range.
需要说明的是,在根据该目标指令中包括的第一访存地址,生成对应的第二访存地址之后,可以将该第一访存地址所对应的内存片区配置为不可访问,这样可以在程序逻辑转移到使用新生成的第二访存地址进行数据访问时,可以防止程序的其他线程访问到该第一访存地址,从而避免读取或写入错误的数据,引发程序崩溃、数据损坏等问题。例如,在一个复杂的多线程程序中,不同线程会共享内存资源,若不将旧的访存地址区域设置为不可访问,会出现某个线程误操作该区域,导致数据不一致。It should be noted that after generating the corresponding second memory access address based on the first memory access address included in the target instruction, the memory area corresponding to the first memory access address can be configured as inaccessible. This can prevent other threads of the program from accessing the first memory access address when the program logic shifts to using the newly generated second memory access address for data access, thereby avoiding reading or writing erroneous data, causing program crashes, data corruption, and other problems. For example, in a complex multi-threaded program, different threads share memory resources. If the old memory access address area is not set to inaccessible, a thread may mistakenly operate on the area, resulting in data inconsistency.
具体的,在程序的其他线程在访问该第一访存地址的情况下,若操作系统识别到该第一访存地址是由于执行本申请所包括的方案而导致的不可访问时,会为第一访存地址提供一个访问错误信号,以供程序的其他线程在访问到该第一访存地址时能够触发该访问错误信号,在操作系统检测到该访问错误信号时,可以控制程序的其他线程处于停止等待状态,并且周期性继续访问该第一访存地址,直至不再触发该访问错误信号后正常访问该第一访存地址。Specifically, when other threads of the program access the first memory access address, if the operating system recognizes that the first memory access address is inaccessible due to the execution of the scheme included in this application, it will provide an access error signal for the first memory access address so that other threads of the program can trigger the access error signal when accessing the first memory access address. When the operating system detects the access error signal, it can control the other threads of the program to be in a stop waiting state, and continue to access the first memory access address periodically until the access error signal is no longer triggered and the first memory access address is accessed normally.
这样,本申请可以根据该目标指令中包括的第一访存地址,生成对应的第二访存地址,并将该第一访存地址所对应的内存片区配置为不可访问,这样就可以在执行的过程中,仅由当前的指令执行逻辑对目标指令中包括的第一访存地址所对应的内存数据进行操作,而其他涉及目标指令中对应的内存数据的指令执行逻辑均无法操作,这样就可以避免了不同指令执行逻辑同时对同一数据进行操作会引发的数据竞争问题。In this way, the present application can generate a corresponding second memory access address based on the first memory access address included in the target instruction, and configure the memory slice corresponding to the first memory access address as inaccessible. In this way, during the execution process, only the current instruction execution logic can operate on the memory data corresponding to the first memory access address included in the target instruction, and other instruction execution logics involving the corresponding memory data in the target instruction cannot operate. In this way, the data competition problem caused by different instruction execution logics operating on the same data at the same time can be avoided.
示例的,每个线程都有自己的地址空间或者在共享地址空间内有明确的访问范围划分。当一个线程因为访存对齐异常重新映射到第二访存地址进行访存操作时,它的操作是在自身的内存访问权限和范围内进行的,不会干扰到其他线程对其他访存地址的正常访问。例如,在多线程的数据库管理系统中,一个线程在处理数据查询时遇到访存对齐异常并重新映射地址访问数据,而其他线程可能在对其他访存地址进行数据插入、更新等操作,它们之间的内存访问相互独立,互不影响,所以无需停止其他线程。For example, each thread has its own address space or a clear access range within the shared address space. When a thread is remapped to a second memory address for memory access due to a memory alignment exception, its operation is performed within its own memory access permissions and range, and will not interfere with other threads' normal access to other memory addresses. For example, in a multi-threaded database management system, a thread encounters a memory alignment exception when processing a data query and remaps the address to access data, while other threads may be inserting or updating data at other memory addresses. The memory access between them is independent of each other and does not affect each other, so there is no need to stop other threads.
由于只有当前的指令执行逻辑能够操作第二访存地址对应的数据,因此可以确保数据在被访问和修改时的一致性,使得程序在处理这些数据时不会因为多个执行逻辑的干扰而产生错误的结果,而且还无需停止其他指令执行逻辑的继续执行,对于不涉及第一访存地址数据操作的其他线程可以继续执行不受影响的任务,从而避免了不必要的线程阻塞,提高了整个系统的并发性能和源程序指令翻译执行的效率。Since only the current instruction execution logic can operate the data corresponding to the second memory access address, the consistency of the data can be ensured when it is accessed and modified, so that the program will not produce erroneous results due to the interference of multiple execution logics when processing these data. There is no need to stop the continued execution of other instruction execution logics. Other threads that do not involve the first memory access address data operation can continue to execute unaffected tasks, thereby avoiding unnecessary thread blocking and improving the concurrency performance of the entire system and the efficiency of source program instruction translation and execution.
采用上述技术方案,程序的多线程在面对内存访问地址状态变化时,能够有序地进行处理,既保证了在地址不可访问时不会盲目操作,又能在地址恢复可用时及时恢复正常访问。By adopting the above technical solution, the multi-threaded program can handle the changes in the memory access address status in an orderly manner, which not only ensures that there will be no blind operation when the address is inaccessible, but also ensures that normal access can be restored in time when the address becomes available again.
步骤103,针对该第二访存地址执行访存操作,以访问该第二访存地址的数据。Step 103: Perform a memory access operation on the second memory access address to access data at the second memory access address.
采用上述技术方案,可以通过根据存在访存对齐异常的第一访存地址来重新映射内存片区的访存地址,将非对齐访存转换为基于新的地址(第二访存地址)的访存操作,这样可以使得整个访存过程作为一个原子操作来执行,避免了被其他线程或进程干扰;并将该第一访存地址所对应的内存片区配置为不可访问,本方案无需停止其他线程,而是通过重新映射内存的方式解决访存对齐问题,对于不涉及第一内存片区中数据操作的其他线程可以继续执行不受影响的任务,减少了不必要的线程阻塞和调度开销,从而提高了整体的执行效率,提高了整个系统的并发性能和源程序指令翻译执行的效率。By adopting the above technical solution, the memory access address of the memory slice can be remapped according to the first memory access address with memory access alignment exception, and the non-aligned memory access can be converted into a memory access operation based on the new address (the second memory access address). In this way, the entire memory access process can be executed as an atomic operation, avoiding interference from other threads or processes; and the memory slice corresponding to the first memory access address is configured as inaccessible. This solution does not need to stop other threads, but solves the memory alignment problem by remapping the memory. For other threads that are not involved in data operations in the first memory slice, they can continue to execute unaffected tasks, reducing unnecessary thread blocking and scheduling overhead, thereby improving the overall execution efficiency, improving the concurrency performance of the entire system and the efficiency of source program instruction translation and execution.
图2是本申请实施例提供的一种二进制翻译方法的流程图,包括如下步骤:FIG2 is a flowchart of a binary translation method provided in an embodiment of the present application, comprising the following steps:
步骤201,对客户机中源程序包括的每个基本块进行翻译,将每个该基本块包括的指令,翻译为与宿主机架构对应的指令。Step 201 : Translate each basic block included in the source program in the client, and translate the instructions included in each basic block into instructions corresponding to the host architecture.
其中,源程序可以表征基于源架构平台(客户机架构平台)开发的应用程序。也即,源程序是基于第一指令集架构开发的程序且包括多个基本块,该基本块是指该源程序中顺序执行的一段指令序列。The source program may represent an application developed based on a source architecture platform (client architecture platform). That is, the source program is a program developed based on a first instruction set architecture and includes a plurality of basic blocks, where a basic block refers to a sequence of instructions executed sequentially in the source program.
首先,在需要将基于源架构平台开发的源程序运行在目标架构平台时,可以针对该源程序中包括的每一个该基本块,根据实际执行到该基本块的指令,实时把该基本块中的源架构指令翻译成目标架构指令。First, when it is necessary to run the source program developed based on the source architecture platform on the target architecture platform, the source architecture instructions in each basic block included in the source program can be translated into target architecture instructions in real time based on the instructions actually executed on the basic block.
示例的,对于每个基本块中的指令,可以根据客户机和宿主机的指令集手册,将客户机指令映射到宿主机指令。For example, for instructions in each basic block, the guest instructions may be mapped to host instructions according to the instruction set manuals of the guest and host machines.
具体的,首先可以将客户机指令的操作码转换为宿主机指令的操作码。例如,客户机的ADD(加和)指令对应宿主机的ADD指令。Specifically, the opcode of the client instruction can be first converted to the opcode of the host instruction. For example, the ADD instruction of the client corresponds to the ADD instruction of the host.
然后可以对客户机指令的操作数进行转换,该操作数为用于处理指令的操作数,可以包括寄存器、内存地址、以及立即数等。可以根据宿主机的寄存器命名规则和内存访问方式进行转换。The operands of the guest instructions can then be converted. The operands are operands used to process the instructions, and may include registers, memory addresses, and immediate values. The conversion can be performed based on the host's register naming rules and memory access methods.
示例的,针对寄存器操作数,由于不同的指令集架构通常有不同的寄存器命名和数量。例如,ARM架构有R0-R15等通用寄存器,而x86架构有EAX、EBX、ECX、EDX等通用寄存器,因此可以建立寄存器映射表,将客户机的寄存器名称映射到宿主机的寄存器名称,得到宿主机指令中的寄存器操作数;针对内存操作数,由于不同架构的内存访问方式(如字节序、对齐要求)、内存地址的表示和计算方式不同。例如,有些架构采用大端字节序,而有些采用小端字节序;有些架构对内存访问的地址对齐有严格要求。因此如果客户机和宿主机的字节序不同,可以在进行内存读写操作时需要进行字节序转换,以及如果客户机和宿主机的内存地址的表示和计算方式不同,可以根据宿主机的内存地址计算规则,调整客户机指令中的内存地址。例如,有些架构的内存地址需要加上基址寄存器的值或进行偏移计算。For example, register operands vary depending on the instruction set architecture. Different instruction set architectures typically have different register names and numbers. For example, the ARM architecture has general-purpose registers such as R0-R15, while the x86 architecture has general-purpose registers such as EAX, EBX, ECX, and EDX. Therefore, a register mapping table can be established to map guest register names to host register names, thereby obtaining register operands in host instructions. Regarding memory operands, different architectures have different memory access methods (such as endianness and alignment requirements), as well as different memory address representation and calculation methods. For example, some architectures use big-endian byte order, while others use little-endian byte order. Some architectures also have strict address alignment requirements for memory accesses. Therefore, if the guest and host have different endianness, endian conversion may be required during memory read and write operations. Furthermore, if the guest and host have different memory address representation and calculation methods, the memory address in the guest instruction can be adjusted according to the host's memory address calculation rules. For example, some architectures require adding the value of a base register or performing an offset calculation to the memory address.
在对客户机指令的操作码和操作数进行转换后,可以继续进行指令序列的控制流处理,对于跳转指令和分支指令,需要确保翻译后的指令能够正确实现相同的控制流逻辑。例如,客户机的JMP指令(跳转指令)需要转换为宿主机的相应跳转指令,并且目标地址需要进行正确的转换。After translating the opcode and operands of the guest instructions, control flow processing of the instruction sequence can proceed. For jump and branch instructions, it is necessary to ensure that the translated instructions correctly implement the same control flow logic. For example, the guest's JMP instruction (jump instruction) must be translated into the corresponding jump instruction on the host, and the target address must be correctly translated.
在采用上述操作流程的情况下,可以实现将每个该基本块包括的指令,翻译为与宿主机架构对应的指令。When the above operation flow is adopted, the instructions included in each basic block can be translated into instructions corresponding to the host machine architecture.
步骤202,获取该翻译器当前翻译的指令中的访存地址的编码长度。Step 202: Obtain the encoding length of the memory access address in the instruction currently translated by the translator.
在本步骤中,由于不同指令集架构有着不同的指令编码格式,因此可以依据当前客户机架构对应的指令集的规范来解析当前翻译的指令,从而确定访存地址在指令中的位置和编码方式,并在解析指令之后,找出代表访存地址的字段。其中,该字段的位置和长度因架构而异,因此在确定出访存地址字段的情况下,就可以直接识别出该访存地址的编码长度。In this step, since different instruction set architectures have different instruction encoding formats, the currently translated instruction can be parsed according to the specification of the instruction set corresponding to the current client architecture to determine the location and encoding method of the memory access address in the instruction. After parsing the instruction, the field representing the memory access address is found. The location and length of this field vary depending on the architecture, so once the memory access address field is determined, the encoding length of the memory access address can be directly identified.
例如,以一条x86内存访问指令0x8B 0x0D 0x12 0x34 0x56 0x78为例,这是一条MOV指令,用于将内存中的数据移动到寄存器。For example, take an x86 memory access instruction 0x8B 0x0D 0x12 0x34 0x56 0x78 as an example. This is a MOV instruction used to move data from memory to a register.
对于0x8B,该操作码通常对应MOV指令,该指令的功能是将一个操作数的值移动到另一个操作数中。For 0x8B, this opcode usually corresponds to the MOV instruction, which moves the value of one operand to another operand.
对于0x0D,二进制表示为00001101,其中,Mod=00,Reg=001,R/M=101,根据Mod和R/M的组合,这里表示使用一个32位的偏移量来计算内存地址,Reg=001通常对应寄存器ECX,这意味着目标寄存器是ECX。For 0x0D, the binary representation is 00001101, where Mod = 00, Reg = 001, and R/M = 101. According to the combination of Mod and R/M, this indicates that a 32-bit offset is used to calculate the memory address. Reg = 001 usually corresponds to register ECX, which means that the target register is ECX.
对于0x12 0x34 0x56 0x78,表示偏移量为0x78563412,则在获取“0x8B 0x0D0x12 0x34 0x56 0x78”的编码长度的情况下,可以确定出该条指令中的初始访存地址的编码长度为32位(4个字节)。For 0x12 0x34 0x56 0x78, it means the offset is 0x78563412. Then, when the encoding length of "0x8B 0x0D 0x12 0x34 0x56 0x78" is obtained, it can be determined that the encoding length of the initial memory access address in this instruction is 32 bits (4 bytes).
步骤203,在该编码长度大于或者等于该宿主机架构对应的编码长度时,确定该当前翻译的指令存在访存对齐异常。Step 203: When the encoding length is greater than or equal to the encoding length corresponding to the host architecture, it is determined that a memory access alignment exception exists in the currently translated instruction.
其中,该宿主机架构对应的编码长度是指运行翻译后指令的目标计算机系统的架构其自身规定的访存地址编码长度,这是由该架构的硬件设计和内存管理机制决定的。例如,在32位的x86架构中,通常使用32位编码来表示访存地址;而在64位的x86-64架构中,则使用64位编码。The encoding length corresponding to the host architecture refers to the memory address encoding length specified by the architecture of the target computer system running the translated instructions. This is determined by the architecture's hardware design and memory management mechanisms. For example, in the 32-bit x86 architecture, 32-bit encoding is typically used to represent memory addresses, while in the 64-bit x86-64 architecture, 64-bit encoding is used.
在获取的当前翻译的指令中包括的访存地址的编码长度大于或者等于该宿主机架构对应的编码长度时,如x86-64中的cmpxchg16b指令(在x86-64架构中用于进行16字节(128位)比较并交换操作的指令),可以最高支持128位(16字节)的原子指令。在将基于x86-64的指令翻译至RISC-V架构上运行的情况下,存在翻译后RISC-V 架构下的指令无法保证指令的原子性的情况。When the encoding length of the memory address included in the currently translated instruction is greater than or equal to the encoding length corresponding to the host architecture, such as the x86-64 instruction cmpxchg16b (which performs a 16-byte (128-bit) compare-and-swap operation), atomic instructions of up to 128 bits (16 bytes) can be supported. When translating x86-64 instructions to the RISC-V architecture, there are cases where the atomicity of the translated RISC-V instructions cannot be guaranteed.
步骤204,将该当前翻译的指令中包括的初始访存地址存储于既定预设寄存器。Step 204: Store the initial memory access address included in the currently translated instruction in a predetermined preset register.
在本步骤中,若确定当前翻译的指令存在访存对齐异常,则可以将该当前翻译的指令所包括的访存地址存储于既定预设寄存器中。In this step, if it is determined that the currently translated instruction has a memory access alignment exception, the memory access address included in the currently translated instruction may be stored in a predetermined preset register.
其中,寄存器是CPU(处理器)内部的高速存储单元,能够快速地被CPU访问。将访存地址存于寄存器,可使CPU迅速获取该地址,为后续生成停止指令以及执行相关操作提供便利。Registers are high-speed storage units within the CPU (processor) that can be quickly accessed by the CPU. Storing the memory address in a register allows the CPU to quickly retrieve the address, facilitating the subsequent generation of stop instructions and execution of related operations.
步骤205,在该翻译结果中,在该当前翻译的指令翻译后得到的对应指令位置处,写入包括预设非对齐访存地址的停止指令。Step 205 : Writing a stop instruction including a preset non-aligned memory access address into the translation result at the corresponding instruction position obtained after the currently translated instruction is translated.
其中,该停止指令的目的是在特定时刻中断程序的正常执行流程。通过将其访存地址设置为预设非对齐访存地址,可利用系统对非对齐内存访问的处理机制来触发异常。The purpose of the stop instruction is to interrupt the normal execution flow of the program at a specific moment. By setting its memory access address to a preset non-aligned memory access address, the system's handling mechanism for non-aligned memory access can be used to trigger an exception.
在本步骤中,在将当前翻译的指令中包括的访存地址存储于该寄存器后,可以生成一条停止指令,该停止指令中对应的访存地址为预先设置的一个预设非对齐访存地址,在执行该翻译结果时,执行该停止指令,在访问该预设非对齐访存地址时会触发一个执行停止信号,其中,该执行停止信号为SIGBUS信号(总线错误信号)。触发到SIGBUS信号时,可以停止执行该翻译结果。In this step, after storing the memory access address included in the currently translated instruction in the register, a stop instruction may be generated. The memory access address corresponding to the stop instruction is a preset non-aligned memory access address. When the translation result is executed, the stop instruction is executed. When the preset non-aligned memory access address is accessed, an execution stop signal is triggered. The execution stop signal is a SIGBUS signal (bus error signal). When the SIGBUS signal is triggered, execution of the translation result may be stopped.
考虑到在一些宿主机架构中,对内存访问地址的对齐是有要求,若当前翻译的指令中包括的访存地址存在访存对齐异常,但是在这些宿主机架构执行翻译后的指令序列时,若直接执行当前指令在翻译后对应的指令,宿主机架构的系统不会强制终止程序,而是继续执行后续的指令。然而,这种情况下程序会基于错误的内存数据进行计算,从而产生错误的结果。Considering that some host architectures require memory access address alignment, if the memory access address included in the current translated instruction has a memory access alignment anomaly, when executing the translated instruction sequence on these host architectures, if the corresponding instruction after the current instruction is directly executed, the host architecture system will not forcibly terminate the program and will continue to execute subsequent instructions. However, in this case, the program will perform calculations based on incorrect memory data, resulting in incorrect results.
因此可以在当前翻译的指令中包括的访存地址存在访存对齐异常的情况下,在当前翻译的指令翻译后得到的对应指令位置处,写入包括该预设非对齐访存地址的停止指令,这样在这些宿主机架构执行翻译后的指令序列时,在翻译后的指令位置处同步执行到该停止指令,则中止程序的正常执行流程,以避免程序会基于错误的内存数据进行计算,从而防止产生错误的结果。Therefore, when there is a memory access alignment exception in the memory access address included in the currently translated instruction, a stop instruction including the preset non-aligned memory access address can be written at the corresponding instruction position obtained after the translation of the currently translated instruction. In this way, when these host architectures execute the translated instruction sequence, the stop instruction is synchronously executed at the translated instruction position, and the normal execution process of the program is terminated to avoid the program from performing calculations based on erroneous memory data, thereby preventing erroneous results.
步骤206,将该停止指令作为该目标指令。Step 206: Use the stop instruction as the target instruction.
步骤207,在该当前翻译的指令中的访存地址的地址类型为非对齐访存地址时,确定该当前翻译的指令存在访存对齐异常。Step 207 : When the address type of the memory access address in the currently translated instruction is a non-aligned memory access address, it is determined that the currently translated instruction has a memory access alignment exception.
在本步骤中,首先可以检测到当前翻译的指令中包括的访存地址是否为非对齐访存地址。In this step, it is first detected whether the memory access address included in the currently translated instruction is a non-aligned memory access address.
示例的,在指令翻译过程中,系统会对每条涉及内存访问的指令进行检查。检查的方式通常是计算指令的访存地址对相应数据类型对齐字节数取模,如果结果不为零,就说明该访存地址为非对齐访存地址,此时这条指令就是存在访存对齐异常的目标指令。For example, during instruction translation, the system checks each instruction involving memory access. This check typically involves calculating the instruction's memory access address modulo the number of bytes aligned for the corresponding data type. If the result is non-zero, it indicates that the memory access address is unaligned, and the instruction is the target of a memory access alignment exception.
例如,对于一个32位整数的访问指令,若其访存地址对4取模不为0,则表示该地址不是4的整数倍,即该地址不是按照4字节对齐的方式进行访问的,即可判定该指令存在访存对齐异常。For example, for an instruction accessing a 32-bit integer, if the memory access address modulo 4 is not 0, it means that the address is not an integer multiple of 4, that is, the address is not accessed in a 4-byte aligned manner, and it can be determined that the instruction has a memory access alignment exception.
然后在检测到当前翻译的指令中包括的访存地址为非对齐的访存地址时,确定当前翻译的指令存在访存对齐异常。Then, when it is detected that the memory access address included in the currently translated instruction is a non-aligned memory access address, it is determined that the currently translated instruction has a memory access alignment exception.
步骤208,对该当前翻译的指令进行翻译,得到该目标指令。Step 208: Translate the currently translated instruction to obtain the target instruction.
在本步骤中,可以按照访存地址对齐的翻译方法,对当前翻译的指令进行翻译,得到包括非对齐的访存地址的该目标指令。In this step, the currently translated instruction may be translated according to a translation method for memory access address alignment to obtain the target instruction including a non-aligned memory access address.
考虑到在一些宿主机架构中,虽然对内存访问地址的对齐是有要求,但是仍具备一定程度支持非对齐内存访问的能力。当遇到存在访存对齐异常的指令时,硬件能够自动对非对齐访问进行处理。例如,它可以将一次非对齐的内存访问拆分成多次对齐的内存访问操作,从不同的内存块中读取或写入数据,然后再进行组合。Considering that some host architectures, while requiring memory access address alignment, still have some degree of support for unaligned memory accesses, when encountering instructions with memory access alignment exceptions, the hardware can automatically handle unaligned accesses. For example, it can split a single unaligned memory access into multiple aligned memory access operations, reading or writing data from different memory blocks, and then recombining them.
因此,可以继续按照访存地址对齐的翻译方法,对当前翻译的指令进行翻译,得到包括非对齐的访存地址的该目标指令。Therefore, the currently translated instruction may continue to be translated according to the translation method of memory access address alignment to obtain the target instruction including the non-aligned memory access address.
虽然此处得到的该目标指令中仍包括非对齐的访存地址,在这些宿主机架构执行翻译后的指令序列时,可以直接执行当前指令在翻译后对应的指令,程序在执行时不会因为访存对齐异常而立即停止,但由于多次访问和数据组合的过程,会导致数据读取或写入的结果出错,影响程序的正确性。Although the target instruction obtained here still includes non-aligned memory access addresses, when these host architectures execute the translated instruction sequence, they can directly execute the instructions corresponding to the current instruction after translation. The program will not stop immediately due to memory alignment exceptions during execution. However, due to the process of multiple accesses and data combinations, the results of data reading or writing will be erroneous, affecting the correctness of the program.
但是结合本申请中步骤102的实施方式,即可消除此处出现的问题,这是因为本申请可以根据该目标指令中包括的第一访存地址,生成对应的第二访存地址,并将该第一访存地址所对应的内存片区配置为不可访问,这样就可以在执行的过程中,仅由当前的指令执行逻辑对目标指令中包括的第一访存地址所对应的内存数据进行操作,而其他涉及目标指令中对应的内存数据的指令执行逻辑均无法操作,这样就可以避免了不同指令执行逻辑同时对同一数据进行操作会引发的数据竞争问题。However, in combination with the implementation method of step 102 in the present application, the problem here can be eliminated. This is because the present application can generate a corresponding second memory access address based on the first memory access address included in the target instruction, and configure the memory slice corresponding to the first memory access address to be inaccessible. In this way, during the execution process, only the current instruction execution logic can operate on the memory data corresponding to the first memory access address included in the target instruction, and other instruction execution logics involving the corresponding memory data in the target instruction cannot operate. In this way, the data competition problem caused by different instruction execution logics operating on the same data at the same time can be avoided.
示例的,每个线程都有自己的地址空间或者在共享地址空间内有明确的访问范围划分。当一个线程因为访存对齐异常重新映射到第二访存地址进行访存操作时,它的操作是在自身的内存访问权限和范围内进行的,不会干扰到其他线程对其他访存地址的正常访问。例如,在多线程的数据库管理系统中,一个线程在处理数据查询时遇到访存对齐异常,并重新映射地址访问数据,而其他线程可能在对其他访存地址进行数据插入、更新等操作,它们之间的内存访问相互独立,互不影响,所以无需停止这些对其他访存地址进行数据插入、更新的线程。For example, each thread has its own address space or a clear access range division within the shared address space. When a thread is remapped to a second memory access address for memory access due to a memory alignment exception, its operation is performed within its own memory access rights and range, and will not interfere with other threads' normal access to other memory addresses. For example, in a multi-threaded database management system, a thread encounters a memory alignment exception when processing a data query and remaps the address to access data, while other threads may be inserting or updating data at other memory addresses. The memory access between them is independent of each other and does not affect each other, so there is no need to stop these threads that are inserting or updating data at other memory addresses.
由于只有当前的指令执行逻辑能够操作第二访存地址对应的数据,因此可以确保数据在被访问和修改时的一致性,使得程序在处理这些数据时不会因为多个执行逻辑的干扰而产生错误的结果,而且还无需停止其他指令执行逻辑的继续执行,对于不涉及第一访存地址数据操作的其他线程可以继续执行不受影响的任务,从而避免了不必要的线程阻塞,提高了整个系统的并发性能和源程序指令翻译执行的效率。Since only the current instruction execution logic can operate the data corresponding to the second memory access address, the consistency of the data can be ensured when it is accessed and modified, so that the program will not produce erroneous results due to the interference of multiple execution logics when processing these data. There is no need to stop the continued execution of other instruction execution logics. Other threads that do not involve the first memory access address data operation can continue to execute unaffected tasks, thereby avoiding unnecessary thread blocking and improving the concurrency performance of the entire system and the efficiency of source program instruction translation and execution.
图3是本申请实施例提供的另一种二进制翻译方法,包括如下步骤:FIG3 is another binary translation method provided in an embodiment of the present application, comprising the following steps:
步骤301,从翻译器的缓存中获取每个基本块的翻译结果。Step 301: Obtain the translation result of each basic block from the cache of the translator.
在本步骤中,实现方式可以如上述步骤101的实现方式所示,具体的实施步骤在此不再赘述。In this step, the implementation method can be shown in the implementation method of the above step 101, and the specific implementation steps are not repeated here.
步骤302,在对该翻译结果中的指令进行执行的过程中,同步检测执行停止信号。Step 302: During the execution of the instructions in the translation result, synchronously detect an execution stop signal.
在本步骤中,可以对该翻译结果中的当前的指令进行执行,然后可以在行执行的过程中,检测执行停止信号。In this step, the current instruction in the translation result may be executed, and then an execution stop signal may be detected during the execution process.
由于在指令执行期间,会出现各种异常情况,如内存访问错误、除零错误等,这些异常往往会触发相应的执行停止信号。通过检测这些信号,程序能够及时发现并响应错误,方便开发者进行调试,定位问题所在。例如,当遇到访存对齐异常触发SIGBUS信号(总线错误信号)时,程序可以捕获该信号并进行相应处理,避免程序崩溃。During instruction execution, various exceptions, such as memory access errors and divide-by-zero errors, can occur. These often trigger corresponding stop-execution signals. By detecting these signals, the program can promptly detect and respond to errors, facilitating debugging and problem location. For example, when a memory alignment exception triggers a SIGBUS (bus error) signal, the program can capture and handle it accordingly, preventing a program crash.
示例的,在对该翻译结果中的当前的指令进行执行的过程中,若由于各种异常情况导致触发执行停止信号,可以采用提供监控程序执行状态的工具和机制,来用于检测执行停止信号。For example, during the execution of the current instruction in the translation result, if an execution stop signal is triggered due to various abnormal situations, a tool and mechanism for monitoring the execution status of the program can be used to detect the execution stop signal.
例如,在调试过程中,可使用调试器(如GDB)来监控程序的执行,来接收到执行停止信号。For example, during debugging, a debugger (such as GDB) may be used to monitor the execution of a program to receive an execution stop signal.
在一些实施例中,在指令执行过程中,系统会对每条涉及内存访问的指令进行检查。检查的方式通常是计算指令的访存地址对相应数据类型对齐字节数取模,如果结果不为零,就说明该访存地址未满足对齐要求,此时这条指令就是存在访存对齐异常的目标指令。或者在对每条涉及内存访问的指令进行检查,如果该指令中的访存地址的位宽大于宿主机架构支持的最大位宽,则表示该访存地址无法在宿主机架构下以正常、有效的方式进行寻址和访问,意味着在宿主机架构的内存管理与访问规则下,该指令试图访问的地址超出了架构所允许的范围,就说明该指令为存在访存对齐异常的目标指令。In some embodiments, during the execution of instructions, the system will check each instruction involving memory access. The checking method is usually to calculate the memory access address of the instruction modulo the number of alignment bytes of the corresponding data type. If the result is not zero, it means that the memory access address does not meet the alignment requirements. At this time, this instruction is the target instruction with a memory access alignment exception. Or when checking each instruction involving memory access, if the bit width of the memory access address in the instruction is greater than the maximum bit width supported by the host architecture, it means that the memory access address cannot be addressed and accessed in a normal and effective manner under the host architecture, which means that under the memory management and access rules of the host architecture, the address that the instruction attempts to access exceeds the range allowed by the architecture, which means that the instruction is a target instruction with a memory access alignment exception.
例如,对于一个32位整数的访问指令,若其访存地址对4取模不为0,则判定该指令存在访存对齐异常。For example, for an instruction accessing a 32-bit integer, if the modulo 4 of the memory access address is not 0, it is determined that the instruction has a memory access alignment exception.
又如,客户机架构使用64位编码表示访存地址,而宿主机是32位架构,只能处理32位的访存地址。当客户机指令中的访存地址超出了32位所能表示的范围时,宿主机无法正确处理该地址,会引发对齐问题,则同样可以判定该指令存在访存对齐异常。For example, the guest architecture uses 64-bit encoding to represent memory addresses, while the host architecture is 32-bit and can only process 32-bit memory addresses. When the memory address in the guest instruction exceeds the range that can be represented by 32 bits, the host cannot correctly process the address, causing alignment issues. Similarly, the instruction can be determined to have a memory alignment exception.
然后可以在检测到该执行停止信号的情况下,若确定触发该执行停止信号的停止指令为存在访存对齐异常的指令,执行步骤303-312。Then, when the execution stop signal is detected, if it is determined that the stop instruction that triggers the execution stop signal is an instruction with a memory access alignment exception, steps 303 to 312 are executed.
在一些实施例中,在检测到该执行停止信号时,可以首先获取生成该执行停止信号的停止指令;若确定触发该执行停止信号的停止指令为未存在访存对齐异常的指令时,可以获取该停止指令对应的关联函数,并执行该停止指令对应的关联函数,从而同样达到停止执行翻译结果的作用。In some embodiments, when the execution stop signal is detected, the stop instruction that generates the execution stop signal can be first obtained; if it is determined that the stop instruction that triggers the execution stop signal is an instruction without a memory alignment exception, the associated function corresponding to the stop instruction can be obtained and executed, thereby also achieving the effect of stopping the execution of the translation result.
因此在检测到该执行停止信号的情况下,若确定触发该执行停止信号的停止指令为不存在访存对齐异常的指令,执行步骤313。Therefore, when the execution stop signal is detected, if it is determined that the stop instruction that triggers the execution stop signal is an instruction without a memory access alignment exception, step 313 is executed.
在确定该目标指令中包括的该第一访存地址的地址类型为预设非对齐访存地址的情况下,执行步骤303、304、306-312;In the case where it is determined that the address type of the first memory access address included in the target instruction is a preset non-aligned memory access address, executing steps 303, 304, and 306-312;
在确定该目标指令中包括的该第一访存地址的地址类型为非对齐访存地址的情况下,执行步骤305-312;In the case where it is determined that the address type of the first memory access address included in the target instruction is an unaligned memory access address, executing steps 305-312;
步骤303,获取预设寄存器中的访存地址。Step 303: Obtain a memory access address in a preset register.
在本步骤中,在确定该目标指令中包括的该第一访存地址的地址类型为预设非对齐访存地址的情况下,可以获取寄存器中存储的访存地址。In this step, when it is determined that the address type of the first memory access address included in the target instruction is a preset non-aligned memory access address, the memory access address stored in the register can be obtained.
其中,该预设非对齐访存地址是预先设定的不符合系统内存对齐规则的地址。该执行停止信号的目的是在特定时刻中断程序的正常执行流程。以及该寄存器中存储的访存地址为该目标指令位置处的指令翻译前在源程序中对应指令所包括的访存地址。The preset unaligned memory access address is a pre-set address that does not comply with the system memory alignment rules. The purpose of the execution stop signal is to interrupt the normal execution flow of the program at a specific moment. The memory access address stored in the register is the memory access address included in the corresponding instruction in the source program before the instruction at the target instruction location is translated.
考虑到该目标指令中包括的第一访存地址是预先设置的预设非对齐访存地址,这是因为该目标指令在翻译前的基本块中对应指令中所包括的初始访存地址的指令位宽大于宿主机架构所支持的最大位宽,虽然在有些情况下能够保证访存地址对齐,但是存在翻译后宿主机指令无法保证指令的原子性的情况,因此在这种情况下,可以将该预设非对齐访存地址存储于寄存器,并在当前翻译的指令翻译后得到的对应指令位置处,写入包括预设非对齐访存地址的指令,这样可以利用预设非对齐访存地址能够触发执行停止信号的特性,来使得后续能够采用本申请的步骤来保证翻译后指令的原子性。Considering that the first memory access address included in the target instruction is a pre-set preset unaligned memory access address, this is because the instruction bit width of the initial memory access address included in the corresponding instruction in the basic block of the target instruction before translation is greater than the maximum bit width supported by the host architecture. Although the memory access address alignment can be guaranteed in some cases, there are cases where the atomicity of the host instruction cannot be guaranteed after translation. Therefore, in this case, the preset unaligned memory access address can be stored in a register, and the instruction including the preset unaligned memory access address can be written to the corresponding instruction position obtained after the current translated instruction is translated. In this way, the characteristic that the preset unaligned memory access address can trigger the execution stop signal can be utilized to enable the subsequent steps of this application to ensure the atomicity of the translated instruction.
在程序设计中,若执行至包括该预设非对齐访存地址的指令时,可以主动触发执行停止信号。当检测到执行停止信号,并且发现该目标指令的访存地址是预设非对齐访存地址时,表明程序遇到了预期中的异常情况,可以按照预期设置的步骤,从该寄存器中获取寄存器中存储的访存地址。In program design, if an instruction including the preset non-aligned memory access address is executed, an execution stop signal can be proactively triggered. When the execution stop signal is detected and it is found that the memory access address of the target instruction is the preset non-aligned memory access address, it indicates that the program has encountered an expected exception. The memory access address stored in the register can be obtained from the register according to the expected set steps.
步骤304,将该访存地址作为该目标指令中的初始访存地址。Step 304: Use the memory access address as the initial memory access address in the target instruction.
可选的,将该寄存器中存储的访存地址作为该目标指令中包括的初始访存地址。Optionally, the memory access address stored in the register is used as the initial memory access address included in the target instruction.
在本步骤中,在指令执行过程中,当检测到执行停止信号,并且确定触发该信号的目标指令包含预设非对齐访存地址时,将目标指令中的访存地址替换为寄存器中存储的访存地址,其中,该目标指令为在当前翻译的指令翻译后得到的对应指令位置处,写入的包括预设非对齐访存地址的停止指令。该预设非对齐访存地址的停止指令采用图2所示实施例中所采用的停止指令。这样可以确保在后续的执行步骤中采用的是源程序中对应的指令其本身需要访问的访存地址,通过获取并使用正确的访存地址,能够保证程序逻辑的连贯性,使程序按照开发者的预期完成各项任务。In this step, during the execution of the instruction, when an execution stop signal is detected and it is determined that the target instruction that triggers the signal contains a preset non-aligned memory access address, the memory access address in the target instruction is replaced with the memory access address stored in the register, wherein the target instruction is a stop instruction including a preset non-aligned memory access address written at the corresponding instruction position obtained after the currently translated instruction is translated. The stop instruction of the preset non-aligned memory access address adopts the stop instruction adopted in the embodiment shown in Figure 2. In this way, it can be ensured that the memory access address that the corresponding instruction in the source program itself needs to access is adopted in the subsequent execution steps. By obtaining and using the correct memory access address, the consistency of the program logic can be guaranteed, so that the program can complete various tasks as expected by the developer.
步骤305,将该第一访存地址作为该目标指令中的初始访存地址。Step 305: Use the first memory access address as the initial memory access address in the target instruction.
在本步骤中,可以在确定该目标指令中包括的该第一访存地址的地址类型为非对齐访存地址的情况下,可以直接将该第一访存地址作为该目标指令中包括的初始访存地址。In this step, when it is determined that the address type of the first memory access address included in the target instruction is a non-aligned memory access address, the first memory access address can be directly used as the initial memory access address included in the target instruction.
步骤306,获取该初始访存地址所对应的内存片区的内存容量。Step 306: Obtain the memory capacity of the memory area corresponding to the initial memory access address.
示例的,可以根据该初始访存地址所对应的内存片区要访问的数据大小,确定需要分配的空闲内存片区的大小。For example, the size of the free memory area to be allocated may be determined according to the size of data to be accessed by the memory area corresponding to the initial memory access address.
例如,在一个系统中,页面大小为4KB(即2^12字节),假设进程当前的初始访存地址为0x00001200,且需要访问从该地址开始的连续3KB数据。由于页面大小为4KB,3KB数据未跨越页面边界,仍然在虚拟页号为1的这一页内,因此确定需要分配的空闲内存片区的大小为一个页面。For example, in a system with a 4KB page size (2^12 bytes), assume that the process's current initial memory access address is 0x00001200 and needs to access 3KB of continuous data starting at this address. Since the page size is 4KB, the 3KB of data does not cross the page boundary and remains within the page with virtual page number 1. Therefore, the size of the free memory segment to be allocated is determined to be one page.
本实施例中,通过以下方式获取该内存数据对应的内存容量:In this embodiment, the memory capacity corresponding to the memory data is obtained in the following manner:
方式一、分析指令操作码;Method 1: Analyze instruction opcodes;
指令的操作码定义了指令要执行的具体操作,不同的操作码对数据的访问方式和数据容量的需求不同。The opcode of an instruction defines the specific operation to be performed by the instruction. Different opcodes have different requirements for data access methods and data capacity.
如果操作码是加载指令,需要进一步查看指令的操作数,确定是加载一个字节、一个字(通常是2字节)、一个双字(通常是4字节)还是更大的数据类型。例如,在某些架构中,LOADB 指令表示加载一个字节的数据,LOADW 表示加载一个字的数据,再如,如果操作码是存储指令,可以根据操作码可以知道要存储的数据大小。例如,STOREQ表示存储一个 8 字节的四字数据。If the opcode is a load instruction, you need to further examine the instruction's operands to determine whether it is loading a byte, a word (usually 2 bytes), a doubleword (usually 4 bytes), or a larger data type. For example, on some architectures, the LOADB instruction indicates a byte load, while LOADW indicates a word load. Alternatively, if the opcode is a store instruction, the size of the data to be stored can be determined from the opcode. For example, STOREQ indicates a store of an 8-byte quadword.
方式二、考虑操作数和寻址模式;Method 2: Consider operands and addressing modes;
指令的操作数和寻址模式可以提供更多关于访存地址和数据容量的信息。The operands and addressing modes of the instruction can provide more information about the memory address and data capacity.
如果指令中有立即数操作数,表示要访问的数据的偏移量或长度。例如,指令LOAD R1, [R2 + #4] 表示从寄存器 R2 的值加上 4 的地址处加载数据,这里的 4 是偏移量,需要结合数据类型来确定加载的数据容量。If an instruction contains an immediate operand, it indicates the offset or length of the data to be accessed. For example, the instruction LOAD R1, [R2 + #4] loads data from the address obtained by adding 4 to the value of register R2. The 4 here is the offset, and the data type must be considered to determine the capacity of the data to be loaded.
在寄存器间接寻址模式下,寄存器中的值是访存地址。此时需要根据指令上下文和其他相关信息来确定要访问的数据范围。例如,如果寄存器R3指向一个数组的首地址,指令LOAD R4,[R3]是加载数组的第一个元素,需要知道数组元素的数据类型才能确定加载的数据容量。In register indirect addressing mode, the register value is the memory address to be accessed. The instruction context and other relevant information are required to determine the data range to be accessed. For example, if register R3 points to the first address of an array, the instruction LOAD R4, [R3] loads the first element of the array. The data type of the array element is required to determine the data capacity to be loaded.
步骤307,在该缓存中查找包括该内存容量在内的空闲内存片区。Step 307: Search the cache for a free memory area including the memory capacity.
可选的,可以使用操作系统提供的系统调用函数(如在Linux系统中用于重新映射内存区域的系统调用的mremap函数)来查找包括该内存容量的空闲内存片区。Alternatively, a system call function provided by the operating system (such as the mremap function of the system call for remapping a memory area in a Linux system) may be used to search for a free memory area that includes the memory capacity.
示例的,首先可以获取该初始访存地址所对应的内存片区的第一起始地址,以及该初始访存地址所对应的内存片区的内存大小,然后可以确定重新映射行为的标志位,其中,该标志位用于控制重新映射函数mremap的行为。常见的标志有包括MREMAP_MAYMOVE标志以及MREMAP_FIXED标志,其中,MREMAP_MAYMOVE标志用于允许操作系统将映射区域移动到新的地址。如果不指定该标志,操作系统就无法在原地址处扩展映射区域,则mremap重新映射会失败;以及MREMAP_FIXED标志用于表示新的映射地址将由额外的参数new_address(地址参数)指定。For example, first, the first starting address of the memory slice corresponding to the initial memory access address and the memory size of the memory slice corresponding to the initial memory access address can be obtained, and then the flag of the remapping behavior can be determined, wherein the flag is used to control the behavior of the remapping function mremap. Common flags include the MREMAP_MAYMOVE flag and the MREMAP_FIXED flag, wherein the MREMAP_MAYMOVE flag is used to allow the operating system to move the mapping area to a new address. If this flag is not specified, the operating system cannot expand the mapping area at the original address, and the mremap remapping will fail; and the MREMAP_FIXED flag is used to indicate that the new mapping address will be specified by the additional parameter new_address (address parameter).
具体的,若在使用mremap函数时设置MREMAP_MAYMOVE标志位,则可以将映射区域(即初始访存地址所对应的内存片区)移动到空闲内存片区,以及若在使用mremap函数时设置MREMAP_FIXED标志位,则可以由额外的参数(new_address)来指定新的映射地址(即空闲内存片区的第二起始地址)。指定新的映射地址时可以调用mremap函数,用于根据初始访存地址所对应的内存片区的内存大小,以及该初始访存地址所对应的内存片区的第一起始地址,将初始访存地址所对应的内存片区移动至空闲内存片区。Specifically, if the MREMAP_MAYMOVE flag is set when using the mremap function, the mapping area (i.e., the memory slice corresponding to the initial memory access address) can be moved to the free memory slice, and if the MREMAP_FIXED flag is set when using the mremap function, the new mapping address (i.e., the second starting address of the free memory slice) can be specified by an additional parameter (new_address). When specifying a new mapping address, the mremap function can be called to move the memory slice corresponding to the initial memory access address to the free memory slice based on the memory size of the memory slice corresponding to the initial memory access address and the first starting address of the memory slice corresponding to the initial memory access address.
其中,在一种可能的实现方式中,在指定MREMAP_FIXED标志的情况下,可以由额外的参数(new_address)来指定新的映射地址(即空闲内存片区的第二起始地址)。In one possible implementation, when the MREMAP_FIXED flag is specified, an additional parameter (new_address) may be used to specify a new mapping address (ie, the second starting address of the free memory area).
在另一种可能的实现方式中,在指定MREMAP_MAYMOVE标志的情况下,操作系统会尝试在其他位置找到足够大的连续空闲内存块,并将原映射区域的数据复制到新的位置,此时新的起始地址会是操作系统找到的空闲内存片区的第二起始地址。In another possible implementation, when the MREMAP_MAYMOVE flag is specified, the operating system will try to find a sufficiently large continuous free memory block at another location and copy the data of the original mapping area to the new location. At this time, the new starting address will be the second starting address of the free memory area found by the operating system.
步骤308,获取该初始访存地址所对应的内存片区的第一起始地址,以及获取该空闲内存片区的第二起始地址。Step 308: Obtain a first starting address of the memory area corresponding to the initial memory access address, and obtain a second starting address of the free memory area.
示例的,该初始访存地址所对应的内存片区以页为单位,页面大小为4KB(即212字节)。假设初始访存地址为0x10023,在获取该初始访存地址所对应的内存片区的第一起始地址时,可以通过地址整除页面大小来计算。例如,页号=0x10023÷4KB=4(这里的除法为整除运算)。第一起始地址=4×4KB=0x10000。也就是说,包含0x10000地址的内存片区(页)的起始地址是0x10000。For example, the memory area corresponding to the initial memory access address is in pages, with a page size of 4KB (i.e., 2 12 bytes). Assuming the initial memory access address is 0x10023, to obtain the first starting address of the memory area corresponding to the initial memory access address, you can calculate it by dividing the address by the page size. For example, page number = 0x10023 ÷ 4KB = 4 (the division here is integer division). The first starting address = 4 × 4KB = 0x10000. In other words, the starting address of the memory area (page) containing address 0x10000 is 0x10000.
以及在获取该空闲内存片区的第二起始地址时,可以首先确定一个空闲内存链表,链表节点存储每个空闲内存片区的起始地址和大小,当需要分配空闲内存片区时,遍历链表找到一个合适大小的空闲内存片区。例如,链表中有一个节点表示的空闲内存片区大小为8KB,起始地址为0x20000。那么0x20000这个就是我们获取的空闲内存片区的第二起始地址。When obtaining the second starting address of the free memory segment, a free memory linked list can be first determined. The linked list nodes store the starting address and size of each free memory segment. When a free memory segment needs to be allocated, the linked list is traversed to find a free memory segment of the appropriate size. For example, if a node in the linked list represents a free memory segment with a size of 8KB and a starting address of 0x20000, then 0x20000 is the second starting address of the free memory segment we obtained.
步骤309,计算该初始访存地址相对于该第一起始地址的偏移量。Step 309: Calculate the offset of the initial memory access address relative to the first start address.
其中,偏移量表示初始访存地址在其所属内存片区内距离起始地址的距离。通过将初始访存地址减去第一起始地址,就可以得到这个偏移量。The offset represents the distance between the initial memory access address and the start address within the memory slice to which it belongs. This offset can be obtained by subtracting the first start address from the initial memory access address.
示例的,假设第一起始地址为0x10000,初始访存地址为0x10023。因此,偏移量=初始访存地址-第一起始地址=0x10023-0x10000=0x23。For example, assuming the first starting address is 0x10000 and the initial memory access address is 0x10023, the offset = initial memory access address - first starting address = 0x10023 - 0x10000 = 0x23.
步骤310,在该第二起始地址的基础上加和该偏移量,得到针对该空闲内存片区的访存地址。Step 310: Add the offset to the second starting address to obtain a memory access address for the free memory area.
其中,该空闲内存片区的访存地址是指在将该初始访存地址所对应的内存片区重新映射至对应的内存片区后,为了能够正确访问重新映射后的内存片区中的数据而生成的地址。The memory access address of the free memory slice refers to an address generated after the memory slice corresponding to the initial memory access address is remapped to the corresponding memory slice in order to correctly access data in the remapped memory slice.
在本步骤中,在生成该空闲内存片区的访存地址之前,需要确定空闲内存片区已成功映射,具体的,可以通过确定mremap操作已成功完成,且得到了有效的空闲内存片区的第二起始地址的方式确定空闲内存片区已成功映射。In this step, before generating the memory access address of the free memory slice, it is necessary to determine that the free memory slice has been successfully mapped. Specifically, it can be determined that the free memory slice has been successfully mapped by determining that the mremap operation has been successfully completed and the second starting address of the valid free memory slice has been obtained.
示例的,可以通过检查mremap函数的返回值的方式,来确定映射操作是否已成功完成,mremap函数调用成功时会返回重新映射后的内存区域的起始地址(即空闲内存片区的第二起始地址),失败时会返回MAP_FAILED(通常被定义为(void *)-1)。因此,可以检查返回值以确定操作是否成功。如果返回的空闲内存片区的第二起始地址不等于MAP_FAILED,则表示mremap映射操作成功,表示对该初始访存地址所对应的内存片区进行重新映射,并已成功映射至空闲内存片区,此时可以继续使用新的映射区域空闲内存片区,否则如果返回的空闲内存片区的第二起始地址等于MAP_FAILED,则表示mremap映射操作失败,表示对该初始访存地址所对应的内存片区进行的重新映射的操作失败。For example, you can check the return value of the mremap function to determine whether the mapping operation has been completed successfully. If the mremap function call is successful, it will return the starting address of the remapped memory area (that is, the second starting address of the free memory segment). If it fails, it will return MAP_FAILED (usually defined as (void *)-1). Therefore, you can check the return value to determine whether the operation is successful. If the second starting address of the returned free memory segment is not equal to MAP_FAILED, it means that the mremap mapping operation is successful, indicating that the memory segment corresponding to the initial memory access address has been remapped and successfully mapped to the free memory segment. At this time, you can continue to use the new mapping area free memory segment. Otherwise, if the second starting address of the returned free memory segment is equal to MAP_FAILED, it means that the mremap mapping operation failed, indicating that the remapping operation of the memory segment corresponding to the initial memory access address failed.
在确定空闲内存片区已成功映射的情况下,可以生成该空闲内存片区的访存地址。When it is determined that the free memory area has been successfully mapped, a memory access address of the free memory area may be generated.
示例的,首先可以确定要访问的访存地址相对于第一起始地址的偏移量,其中,该第一起始地址为初始访存地址所对应的内存片区的起始地址;然后可以将空闲内存片区的第二起始地址与偏移量相加,即可得到该空闲内存片区的访存地址。For example, first, the offset of the memory address to be accessed relative to the first starting address can be determined, where the first starting address is the starting address of the memory slice corresponding to the initial memory access address; then the second starting address of the free memory slice can be added to the offset to obtain the memory address of the free memory slice.
示例的,假设第一起始地址为0x10000,初始访存地址为0x10023,通过计算得出偏移量为0x10023 - 0x10000=0x23。又已知空闲内存片区的第二起始地址为0x20000。那么空闲内存片区的访存地址=第二起始地址+偏移量=0x20000+0x23=0x20023。For example, assuming the first starting address is 0x10000 and the initial memory access address is 0x10023, the offset is calculated to be 0x10023 - 0x10000 = 0x23. Furthermore, the second starting address of the free memory segment is known to be 0x20000. Therefore, the memory access address of the free memory segment = the second starting address + the offset = 0x20000 + 0x23 = 0x20023.
步骤311,将该空闲内存片区的访存地址作为重新映射后的该第二访存地址。Step 311: Use the memory access address of the free memory area as the remapped second memory access address.
步骤312,针对该第二访存地址执行访存操作,以访问该第二访存地址的数据。Step 312: Perform a memory access operation on the second memory access address to access data at the second memory access address.
在本步骤中,若触发执行停止信号的该目标指令,在翻译前的基本块中对应的翻译前指令是一条原子读操作,可以从该第二访存地址重读取数据,然后将数据存储到翻译前指令的目的寄存器中。In this step, if the target instruction that triggers the execution stop signal, the corresponding pre-translation instruction in the pre-translation basic block is an atomic read operation, and the data can be re-read from the second memory access address and then stored in the destination register of the pre-translation instruction.
示例的,每个线程都有自己的地址空间或者在共享地址空间内有明确的访问范围划分。当一个线程因为访存对齐异常重新映射到第二访存地址进行访存操作时,它的操作是在自身的内存访问权限和范围内进行的,不会干扰到其他线程对其他访存地址的正常访问。例如,在多线程的数据库管理系统中,一个线程在处理数据查询时遇到访存对齐异常并重新映射地址访问数据,而其他线程可能在对其他访存地址进行数据插入、更新等操作,它们之间的内存访问相互独立,互不影响,所以无需停止其他线程。For example, each thread has its own address space or a clear access range within the shared address space. When a thread is remapped to a second memory address for memory access due to a memory alignment exception, its operation is performed within its own memory access permissions and range, and will not interfere with other threads' normal access to other memory addresses. For example, in a multi-threaded database management system, a thread encounters a memory alignment exception when processing a data query and remaps the address to access data, while other threads may be inserting or updating data at other memory addresses. The memory access between them is independent of each other and does not affect each other, so there is no need to stop other threads.
若触发执行停止信号的该目标指令,在翻译前的基本块中对应的翻译前指令是一条原子写操作,可以从该翻译前指令的源寄存器中获取数据,然后利用该第二访存地址将数据写入。If the target instruction that triggers the execution stop signal has a corresponding pre-translation instruction in the pre-translation basic block that is an atomic write operation, data can be obtained from the source register of the pre-translation instruction and then written using the second memory access address.
具体的,在将数据写入翻译前指令的目的寄存器,或者在利用该第二访存地址将数据写入时,是利用操作系统的信号处理流程的特点。Specifically, when writing data into the destination register of the pre-translation instruction, or when writing data using the second memory access address, the characteristics of the signal processing flow of the operating system are utilized.
在触发执行停止信号时,操作系统内核会将触发信号的目标指令的寄存器现场放在一片用户态可以访问的内存中,用户程序可以从信号处理函数的参数中访问这片存储寄存器现场的内存,如Linux系统中信号处理函数参数的 ucontext_t 结构可以用来获取寄存器现场信息。在完成信号处理函数时,内核会将这片内存中的寄存器值恢复到硬件寄存器中。When a stop signal is triggered, the operating system kernel places the register context of the target instruction that triggered the signal in a section of memory accessible to user mode. The user program can access this register context from the parameters of the signal handler function. For example, the ucontext_t structure in the signal handler function parameters in Linux can be used to obtain register context information. When the signal handler function completes, the kernel restores the register values in this section of memory to the hardware registers.
因此,本申请可以利用信号处理函数的特点,利用ucontext_t 替代触发异常的宿主机指令完成读写操作,在完成读写之后,恢复原有的内存地址结束信号处理流程,继续执行触发异常指令的下一条指令。Therefore, this application can take advantage of the characteristics of the signal processing function and use ucontext_t to replace the host machine instruction that triggered the exception to complete the read and write operations. After completing the read and write, the original memory address is restored to end the signal processing process and continue to execute the next instruction that triggered the exception.
步骤313,获取该停止指令对应的关联函数,并分别执行该关联函数。Step 313: Obtain the associated function corresponding to the stop instruction, and execute the associated function respectively.
例如,在一个基于x86架构的二进制程序翻译到宿主机架构上运行的情况下,可以将该目标指令中包括的访存地址设置为0xFFFF0000,其是4字节对齐的地址,满足int类型的对齐要求,并且这个地址对应着未分配给当前进程的物理内存页。在x86架构下,虚拟地址需要映射到实际的物理内存页才能进行访问。当尝试访问一个没有有效物理内存映射的地址时,硬件会检测到这个错误并触发SIGBUS信号。在将x86二进制程序翻译到宿主机架构上时,宿主机的内存管理机制无法正确处理这个虚拟地址的映射,从而导致错误。For example, when a binary program based on the x86 architecture is translated and run on the host architecture, the memory access address included in the target instruction can be set to 0xFFFF0000, which is a 4-byte aligned address that meets the alignment requirements of the int type and corresponds to a physical memory page that is not allocated to the current process. Under the x86 architecture, virtual addresses need to be mapped to actual physical memory pages before they can be accessed. When an attempt is made to access an address that does not have a valid physical memory mapping, the hardware will detect this error and trigger a SIGBUS signal. When the x86 binary program is translated to the host architecture, the host's memory management mechanism cannot correctly handle the mapping of this virtual address, resulting in an error.
在此实现方式中,在程序执行翻译结果的过程中,若检测到了执行停止信号,并获取到该目标指令中包括的访存地址设置为0xFFFF0000,该访存地址是4字节对齐的地址,满足int类型的对齐要求,但是对应着未分配给当前进程的物理内存页的情况,该情况会触发执行停止信号,因此可以在程序中查找并执行访存地址0xFFFF0000对应的关联函数,即跳转到错误处理程序,以应对当前的停止信号。In this implementation, during the program execution of the translation result, if an execution stop signal is detected and the memory access address included in the target instruction is obtained and is set to 0xFFFF0000, the memory access address is a 4-byte aligned address, which meets the alignment requirement of the int type, but corresponds to a physical memory page that is not allocated to the current process. This situation will trigger the execution stop signal, so the associated function corresponding to the memory access address 0xFFFF0000 can be found and executed in the program, that is, jumping to the error handler to respond to the current stop signal.
具体的,该停止指令可以触发停止执行信号,该停止执行信号其对应的处理函数包括有具体的处理步骤,因此在该停止指令为未包括存在访存对齐异常的访存地址的指令时,可以获取该停止指令其对应的处理函数所对应的执行步骤,并执行处理函数包括的执行步骤。Specifically, the stop instruction can trigger a stop execution signal, and the processing function corresponding to the stop execution signal includes specific processing steps. Therefore, when the stop instruction is an instruction that does not include a memory access address with a memory access alignment exception, the execution steps corresponding to the processing function corresponding to the stop instruction can be obtained, and the execution steps included in the processing function can be executed.
在本实施例中,由于每个信号在程序中通常都有与之对应的处理函数,因此对于该停止执行信号,其同样关联有一个特定的处理函数,该处理函数包含了一系列具体的操作步骤,这些步骤定义了在接收到停止执行信号后,程序后续对应的执行操作,例如,可以包括保存当前程序运行状态、清理资源、关闭相关连接等操作。In this embodiment, since each signal usually has a corresponding processing function in the program, the stop execution signal is also associated with a specific processing function. The processing function includes a series of specific operation steps. These steps define the subsequent execution operations of the program after receiving the stop execution signal. For example, it may include saving the current program running status, cleaning up resources, closing related connections, and other operations.
例如,可以定义一个名为handle_stop_signal的处理函数,该处理函数中可以包括一系列操作步骤,用于在接收到停止执行信号后,对程序进行相应处理。其中,具体操作步骤如下:For example, you can define a processing function called handle_stop_signal, which can include a series of operation steps for processing the program after receiving the stop execution signal. The specific operation steps are as follows:
S1、保存当前程序运行状态;S1. Save the current program running status;
可以将当前正在处理的数据包编号、处理进度等信息保存到一个日志文件中,以便后续恢复处理。The data packet number currently being processed, processing progress and other information can be saved to a log file for subsequent recovery.
S2、清理资源;S2. Clean up resources;
可以释放线程占用的内存空间、缓冲区等资源。The memory space, buffer and other resources occupied by the thread can be released.
S3、关闭相关连接;S3. Close the relevant connection;
可以关闭与网络服务器的连接,避免资源浪费和数据泄露。You can close the connection with the network server to avoid resource waste and data leakage.
可选的,在一个数据处理程序中,该停止指令可以用于在特定条件下结束线程的运行,当检测到停止指令为未包括存在访存对齐异常的访存地址的指令时,可以执行停止指令对应的处理函数,该函数包括保存未处理完的数据、关闭线程资源等执行步骤。Optionally, in a data processing program, the stop instruction can be used to end the execution of a thread under specific conditions. When it is detected that the stop instruction is an instruction that does not include a memory access address with a memory access alignment exception, the processing function corresponding to the stop instruction can be executed. The function includes execution steps such as saving unprocessed data and closing thread resources.
例如,当程序检测到停止指令时,可以判断该指令的访存地址是否存在对齐异常。假设存在一个线程用于专门负责处理TCP数据包,当接收到停止指令且该指令的访存地址不存在对齐异常时,执行停止指令对应的处理函数。具体步骤如下:For example, when the program detects a stop instruction, it can determine whether the memory access address of the instruction has an alignment exception. Assume that there is a thread dedicated to processing TCP packets. When a stop instruction is received and the memory access address of the instruction does not have an alignment exception, the processing function corresponding to the stop instruction is executed. The specific steps are as follows:
T1、保存未处理完的数据;T1. Save unprocessed data;
可以将当前正在处理的 TCP 数据包的剩余部分保存到一个临时文件中,以便后续重新处理。The remainder of the TCP packet currently being processed can be saved to a temporary file for later reprocessing.
T2、关闭线程资源;T2. Close thread resources;
可以终止该线程的运行,释放线程占用的CPU资源和内存资源。You can terminate the thread and release the CPU and memory resources occupied by the thread.
以及,还可以通过使用一个映射表(如字典、数组等)来存储未存在访存对齐异常的访存地址和对应的错误处理函数之间的关联关系。当检测到停止信号并获取到停止指令的未存在访存对齐异常的访存地址后,通过查询这个映射表,找到对应的错误处理函数并执行。Furthermore, a mapping table (such as a dictionary or array) can be used to store the association between the memory access address without a memory alignment exception and the corresponding error handling function. When a stop signal is detected and the memory access address without a memory alignment exception of the stop instruction is obtained, the corresponding error handling function is found and executed by querying the mapping table.
例如,在使用python编程代码作为示例的情况下,可以使用字典 error_handler_mapping 来存储未存在访存对齐异常的访存地址和错误处理函数的映射关系。当获取到停止指令的未存在访存对齐异常的访存地址后,通过 in 操作符检查该未存在访存对齐异常的访存地址是否存在于字典中,如果存在则调用对应的函数。For example, using Python programming code as an example, the dictionary error_handler_mapping can be used to store the mapping between the memory access address of the memory alignment exception and the error handling function. After obtaining the memory access address of the memory alignment exception of the stop instruction, the in operator is used to check whether the memory access address of the memory alignment exception does not exist in the dictionary. If so, the corresponding function is called.
以及在使用C语言编程代码作为示例的情况下,可以定义了一个结构体ErrorHandlerMapping来存储未存在访存对齐异常的访存地址和对应的函数指针,以及使用一个数组error_handler_mappings来存储所有的映射关系。这样,可以通过遍历数组,来查找与停止指令匹配的项,并调用对应的函数。In the example of C language programming code, a structure called ErrorHandlerMapping is defined to store the memory address and corresponding function pointer for memory alignment exceptions, and an array called error_handler_mappings is used to store all mappings. This way, the array can be traversed to find the item that matches the stop instruction and call the corresponding function.
通过这种方式,可以灵活处理不同来源的执行停止信号,确保程序在各种情况下都能正确响应和处理。In this way, execution stop signals from different sources can be handled flexibly, ensuring that the program can respond and handle correctly in various situations.
综上,在本申请的二进制翻译方法中,可以通过根据存在访存对齐异常的第一访存地址来重新映射内存片区的访存地址,将非对齐访存转换为基于新的地址(第二访存地址)的访存操作,这样可以使得整个访存过程作为一个原子操作来执行,避免了被其他线程或进程干扰;并将该第一访存地址所对应的内存片区配置为不可访问,本方案无需停止其他线程,而是通过重新映射内存的方式解决访存对齐问题,对于不涉及第一内存片区中数据操作的其他线程可以继续执行不受影响的任务,减少了不必要的线程阻塞和调度开销,从而提高了整体的执行效率,提高了整个系统的并发性能和源程序指令翻译执行的效率。In summary, in the binary translation method of the present application, the memory access address of the memory slice can be remapped according to the first memory access address with a memory access alignment exception, and the non-aligned memory access can be converted into a memory access operation based on the new address (the second memory access address). In this way, the entire memory access process can be executed as an atomic operation, avoiding interference from other threads or processes; and the memory slice corresponding to the first memory access address is configured as inaccessible. This solution does not need to stop other threads, but solves the memory alignment problem by remapping the memory. Other threads that are not involved in data operations in the first memory slice can continue to execute unaffected tasks, reducing unnecessary thread blocking and scheduling overhead, thereby improving the overall execution efficiency, the concurrency performance of the entire system, and the efficiency of source program instruction translation and execution.
图4是本申请实施例提供的一种二进制翻译装置的框图,如图4所示,该装置400包括:FIG4 is a block diagram of a binary translation device provided in an embodiment of the present application. As shown in FIG4 , the device 400 includes:
第一获取模块401,用于从翻译器的缓存中获取每个基本块的翻译结果;A first acquisition module 401 is configured to acquire the translation result of each basic block from the cache of the translator;
生成模块402,用于在执行该翻译结果中的指令时,若确定当前执行的指令为存在访存对齐异常的目标指令,则根据该目标指令中包括的第一访存地址进行重新映射,生成第二访存地址,并将该第一访存地址所对应的内存片区配置为不可访问;A generating module 402 is configured to, when executing an instruction in the translation result, if it is determined that the currently executed instruction is a target instruction with a memory access alignment exception, remap the target instruction according to a first memory access address included in the target instruction to generate a second memory access address, and configure a memory slice corresponding to the first memory access address as inaccessible;
执行模块403,用于针对该第二访存地址执行访存操作,以访问该第二访存地址的数据。The execution module 403 is configured to execute a memory access operation on the second memory access address to access data at the second memory access address.
可选的,该生成模块402,用于在对该翻译结果中的指令进行执行的过程中,同步检测执行停止信号;在检测到该执行停止信号的情况下,若确定触发该执行停止信号的停止指令为存在访存对齐异常的指令,则该当前执行的指令为该目标指令,使得停止执行该翻译结果。Optionally, the generation module 402 is used to synchronously detect an execution stop signal during the execution of the instructions in the translation result; when the execution stop signal is detected, if it is determined that the stop instruction that triggers the execution stop signal is an instruction with a memory access alignment exception, then the currently executed instruction is the target instruction, so that the execution of the translation result is stopped.
可选的,该生成模块402,用于根据该第一访存地址的地址类型,获取该目标指令中的初始访存地址;对该初始访存地址所对应的内存片区进行重新映射,得到该第二访存地址。Optionally, the generation module 402 is configured to obtain an initial memory access address in the target instruction according to the address type of the first memory access address; and remap the memory slice corresponding to the initial memory access address to obtain the second memory access address.
可选的,该生成模块402,用于在该地址类型为预设非对齐访存地址的情况下,获取预设寄存器中的访存地址;该目标指令为在当前翻译的指令翻译后得到的对应指令位置处,写入的包括预设非对齐访存地址的停止指令,该访存地址为该当前翻译的指令中所包括的访存地址;将该访存地址作为该目标指令中的初始访存地址。Optionally, the generation module 402 is used to obtain the memory access address in the preset register when the address type is a preset non-aligned memory access address; the target instruction is a stop instruction including the preset non-aligned memory access address written at the corresponding instruction position obtained after the translation of the currently translated instruction, and the memory access address is the memory access address included in the currently translated instruction; and the memory access address is used as the initial memory access address in the target instruction.
可选的,该生成模块402,用于在该地址类型为非对齐访存地址的情况下,将该第一访存地址作为该目标指令中的初始访存地址。Optionally, the generating module 402 is configured to use the first memory access address as the initial memory access address in the target instruction when the address type is a non-aligned memory access address.
可选的,该生成模块402,用于获取该初始访存地址所对应的内存片区的内存容量;在该缓存中查找包括该内存容量在内的空闲内存片区,并生成该空闲内存片区的访存地址;将该空闲内存片区的访存地址作为重新映射后的该第二访存地址。Optionally, the generation module 402 is used to obtain the memory capacity of the memory slice corresponding to the initial memory access address; search for a free memory slice including the memory capacity in the cache, and generate a memory access address of the free memory slice; and use the memory access address of the free memory slice as the remapped second memory access address.
可选的,该生成模块402,用于获取该初始访存地址所对应的内存片区的第一起始地址,以及获取该空闲内存片区的第二起始地址;根据该第一起始地址和该第二起始地址,得到该空闲内存片区的访存地址。Optionally, the generation module 402 is used to obtain the first starting address of the memory slice corresponding to the initial memory access address, and obtain the second starting address of the free memory slice; and obtain the memory access address of the free memory slice based on the first starting address and the second starting address.
可选的,该装置还包括:Optionally, the device further includes:
第二获取模块,用于获取该翻译器当前翻译的指令中的访存地址的编码长度;A second acquisition module is used to obtain the encoding length of the memory access address in the instruction currently translated by the translator;
第一确定模块,用于在该编码长度大于或者等于该宿主机架构对应的编码长度时,确定该当前翻译的指令存在访存对齐异常;A first determining module is configured to determine that a memory access alignment exception exists in the currently translated instruction when the encoding length is greater than or equal to the encoding length corresponding to the host architecture;
第二确定模块,用于根据该当前翻译的指令确定翻译后的该目标指令。The second determining module is configured to determine the target instruction after translation according to the currently translated instruction.
可选的,该第二确定模块,用于在该当前翻译的指令翻译后得到的对应指令位置处,写入包括预设非对齐访存地址的停止指令;并将该当前翻译的指令中包括的初始访存地址存储于既定预设寄存器;将该停止指令作为该目标指令。Optionally, the second determination module is used to write a stop instruction including a preset non-aligned memory access address at the corresponding instruction position obtained after the translation of the currently translated instruction; and store the initial memory access address included in the currently translated instruction in a predetermined preset register; and use the stop instruction as the target instruction.
可选的,该装置还包括:Optionally, the device further includes:
第三获取模块,用于获取该翻译器当前翻译的指令;A third acquisition module is used to obtain the instruction currently translated by the translator;
第三确定模块,用于在该当前翻译的指令中的访存地址的地址类型为非对齐访存地址时,确定该当前翻译的指令存在访存对齐异常,并对该当前翻译的指令进行翻译,得到该目标指令。The third determining module is used to determine that the currently translated instruction has a memory access alignment exception when the address type of the memory access address in the currently translated instruction is a non-aligned memory access address, and translate the currently translated instruction to obtain the target instruction.
综上,本实施例可以通过根据存在访存对齐异常的第一访存地址来重新映射内存片区的访存地址,将非对齐访存转换为基于新的地址(第二访存地址)的访存操作,这样可以使得整个访存过程作为一个原子操作来执行,避免了被其他线程或进程干扰;并将该第一访存地址所对应的内存片区配置为不可访问,本方案无需停止其他线程,而是通过重新映射内存的方式解决访存对齐问题,对于不涉及第一内存片区中数据操作的其他线程可以继续执行不受影响的任务,减少了不必要的线程阻塞和调度开销,从而提高了整体的执行效率,提高了整个系统的并发性能和源程序指令翻译执行的效率。In summary, this embodiment can remap the memory access address of the memory slice according to the first memory access address with memory access alignment exception, and convert the non-aligned memory access into a memory access operation based on the new address (the second memory access address). In this way, the entire memory access process can be executed as an atomic operation, avoiding interference from other threads or processes; and the memory slice corresponding to the first memory access address is configured as inaccessible. This solution does not need to stop other threads, but solves the memory alignment problem by remapping the memory. Other threads that are not involved in data operations in the first memory slice can continue to execute unaffected tasks, reducing unnecessary thread blocking and scheduling overhead, thereby improving overall execution efficiency, improving the concurrency performance of the entire system and the efficiency of source program instruction translation and execution.
图5是根据一示例性实施例示出的一种电子设备500的框图。例如,电子设备500可以是移动电话,计算机,数字广播终端,消息收发设备,游戏控制台,平板设备,医疗设备,健身设备,个人数字助理等。5 is a block diagram of an electronic device 500 according to an exemplary embodiment. For example, the electronic device 500 may be a mobile phone, a computer, a digital broadcast terminal, a messaging device, a game console, a tablet device, a medical device, a fitness device, a personal digital assistant, etc.
参照图5,电子设备500可以包括以下一个或多个组件:处理组件502,存储器504,电源组件506,多媒体组件508,音频组件510,输入/输出接口512,传感器组件514,以及通信组件516。5 , electronic device 500 may include one or more of the following components: a processing component 502 , a memory 504 , a power component 506 , a multimedia component 508 , an audio component 510 , an input/output interface 512 , a sensor component 514 , and a communication component 516 .
处理组件502通常控制电子设备500的整体操作,诸如与显示,电话呼叫,数据通信,相机操作和记录操作相关联的操作。处理组件502可以包括一个或多个处理器520来执行指令,以完成上述的方法的全部或部分步骤。此外,处理组件502可以包括一个或多个模块,便于处理组件502和其他组件之间的交互。例如,处理组件502可以包括多媒体模块,以方便多媒体组件508和处理组件502之间的交互。The processing component 502 generally controls the overall operation of the electronic device 500, such as operations associated with display, phone calls, data communications, camera operation, and recording operations. The processing component 502 may include one or more processors 520 to execute instructions to perform all or part of the steps of the above-described method. In addition, the processing component 502 may include one or more modules to facilitate interaction between the processing component 502 and other components. For example, the processing component 502 may include a multimedia module to facilitate interaction between the multimedia component 508 and the processing component 502.
存储器504用于存储各种类型的数据以支持在电子设备500的操作。这些数据的示例包括用于在电子设备500上操作的任何应用程序或方法的指令,联系人数据,电话簿数据,消息,图片,多媒体等。The memory 504 is used to store various types of data to support the operation of the electronic device 500. Examples of such data include instructions for any application or method operating on the electronic device 500, contact data, phone book data, messages, pictures, multimedia, etc.
电源组件506为电子设备500的各种组件提供电力。电源组件506可以包括电源管理系统,一个或多个电源,及其他与为电子设备500生成、管理和分配电力相关联的组件。The power supply assembly 506 provides power to the various components of the electronic device 500. The power supply assembly 506 may include a power management system, one or more power supplies, and other components associated with generating, managing, and distributing power to the electronic device 500.
多媒体组件508包括在电子设备500和用户之间的提供一个输出接口的屏幕。在一些实施例中,屏幕可以包括液晶显示器(LCD)和触摸面板(TP)。如果屏幕包括触摸面板,屏幕可以被实现为触摸屏,以接收来自用户的输入信号。触摸面板包括一个或多个触摸传感器以感测触摸、滑动和触摸面板上的手势。触摸传感器可以不仅感测触摸或滑动动作的分界,而且还检测与触摸或滑动操作相关的持续时间和压力。在一些实施例中,多媒体组件508包括一个前置摄像头和/或后置摄像头。当电子设备500处于操作模式,如拍摄模式或多媒体模式时,前置摄像头和/或后置摄像头可以接收外部的多媒体数据。每个前置摄像头和后置摄像头可以是一个固定的光学透镜系统或具有焦距和光学变焦能力。The multimedia component 508 includes a screen that provides an output interface between the electronic device 500 and the user. In some embodiments, the screen may include a liquid crystal display (LCD) and a touch panel (TP). If the screen includes a touch panel, it may be implemented as a touch screen to receive input signals from the user. The touch panel includes one or more touch sensors to sense touches, slides, and gestures on the touch panel. The touch sensors can detect not only the demarcation of a touch or slide action, but also the duration and pressure associated with the touch or slide action. In some embodiments, the multimedia component 508 includes a front-facing camera and/or a rear-facing camera. When the electronic device 500 is in an operating mode, such as a capture mode or a multimedia mode, the front-facing camera and/or the rear-facing camera can receive external multimedia data. Each front-facing camera and the rear-facing camera can have a fixed optical lens system or have a variable focal length and optical zoom capability.
音频组件510用于输出和/或输入音频信号。例如,音频组件510包括一个麦克风(MIC),当电子设备500处于操作模式,如呼叫模式、记录模式和语音识别模式时,麦克风用于接收外部音频信号。所接收的音频信号可以被进一步存储在存储器504或经由通信组件516发送。在一些实施例中,音频组件510还包括一个扬声器,用于输出音频信号。The audio component 510 is used to output and/or input audio signals. For example, the audio component 510 includes a microphone (MIC) that receives external audio signals when the electronic device 500 is in an operating mode, such as a call mode, a recording mode, or a voice recognition mode. The received audio signals may be further stored in the memory 504 or transmitted via the communication component 516. In some embodiments, the audio component 510 also includes a speaker for outputting audio signals.
输入/输出接口512为处理组件502和外围接口模块之间提供接口,上述外围接口模块可以是键盘,点击轮,按钮等。这些按钮可包括但不限于:主页按钮、音量按钮、启动按钮和锁定按钮。The input/output interface 512 provides an interface between the processing component 502 and peripheral interface modules, such as a keyboard, a click wheel, buttons, etc. These buttons may include but are not limited to: a home button, a volume button, a start button, and a lock button.
传感器组件514包括一个或多个传感器,用于为电子设备500提供各个方面的状态评估。传感器组件514可以包括接近传感器,被配置用来在没有任何的物理接触时检测附近物体的存在。传感器组件514还可以包括光传感器,如CMOS或CCD图像传感器,用于在成像应用中使用。在一些实施例中,该传感器组件514还可以包括加速度传感器,陀螺仪传感器,磁传感器,压力传感器或温度传感器。The sensor assembly 514 includes one or more sensors for providing various aspects of status assessment for the electronic device 500. The sensor assembly 514 may include a proximity sensor configured to detect the presence of a nearby object without any physical contact. The sensor assembly 514 may also include an optical sensor, such as a CMOS or CCD image sensor, for use in imaging applications. In some embodiments, the sensor assembly 514 may also include an accelerometer, a gyroscope, a magnetic sensor, a pressure sensor, or a temperature sensor.
通信组件516用于便于电子设备500和其他设备之间有线或无线方式的通信。电子设备500可以接入基于通信标准的无线网络,如WiFi,运营商网络(如2G、3G、4G或5G),或它们的组合。在一个示例性实施例中,通信组件516经由广播信道接收来自外部广播管理系统的广播信号或广播相关信息。在一个示例性实施例中,通信组件516还包括近场通信(NFC)模块,以促进短程通信。例如,在NFC模块可基于射频识别(RFID)技术,红外数据协会(IrDA)技术,超宽带(UWB)技术,蓝牙(BT)技术和其他技术来实现。The communication component 516 is used to facilitate wired or wireless communication between the electronic device 500 and other devices. The electronic device 500 can access a wireless network based on a communication standard, such as WiFi, a carrier network (such as 2G, 3G, 4G, or 5G), or a combination thereof. In an exemplary embodiment, the communication component 516 receives broadcast signals or broadcast-related information from an external broadcast management system via a broadcast channel. In an exemplary embodiment, the communication component 516 also includes a near-field communication (NFC) module to facilitate short-range communication. For example, the NFC module can be implemented based on radio frequency identification (RFID) technology, infrared data association (IrDA) technology, ultra-wideband (UWB) technology, Bluetooth (BT) technology, and other technologies.
在示例性实施例中,电子设备500可以被一个或多个应用专用集成电路(ASIC)、数字信号处理器(DSP)、数字信号处理设备(DSPD)、可编程逻辑器件(PLD)、现场可编程门阵列(FPGA)、控制器、微控制器、微处理器或其他电子元件实现,用于实现本申请实施例提供的一种二进制翻译方法。In an exemplary embodiment, the electronic device 500 may be implemented by one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors, or other electronic components to implement a binary translation method provided in an embodiment of the present application.
在示例性实施例中,还提供了一种包括指令的非临时性计算机可读存储介质,例如包括指令的存储器504,上述指令可由电子设备500的处理器520执行以完成上述方法。例如,非临时性存储介质可以是ROM、随机存取存储器(RAM)、CD-ROM、磁带、软盘和光数据存储设备等。In an exemplary embodiment, a non-transitory computer-readable storage medium including instructions is also provided, such as a memory 504 including instructions, which can be executed by the processor 520 of the electronic device 500 to perform the above method. For example, the non-transitory storage medium can be a ROM, a random access memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, an optical data storage device, etc.
图6是根据一示例性实施例示出的一种电子设备600的框图。例如,电子设备600可以被提供为一服务器。参照图6,电子设备600包括处理组件622,其进一步包括一个或多个处理器,以及由存储器632所代表的存储器资源,用于存储可由处理组件622的执行的指令,例如应用程序。存储器632中存储的应用程序可以包括一个或一个以上的每一个对应于一组指令的模块。此外,处理组件622被配置为执行指令,以执行本申请实施例提供的一种二进制翻译方法。FIG6 is a block diagram of an electronic device 600 according to an exemplary embodiment. For example, the electronic device 600 can be provided as a server. Referring to FIG6 , the electronic device 600 includes a processing component 622, which further includes one or more processors, and a memory resource represented by a memory 632 for storing instructions that can be executed by the processing component 622, such as an application. The application stored in the memory 632 can include one or more modules, each of which corresponds to a set of instructions. In addition, the processing component 622 is configured to execute instructions to perform a binary translation method provided in an embodiment of the present application.
电子设备600还可以包括一个电源组件626被配置为执行电子设备600的电源管理,一个有线或无线网络接口650被配置为将电子设备600连接到网络,和一个输入输出接口658。电子设备600可以操作基于存储在存储器632的操作系统。The electronic device 600 may further include a power supply component 626 configured to perform power management of the electronic device 600, a wired or wireless network interface 650 configured to connect the electronic device 600 to a network, and an input/output interface 658. The electronic device 600 may operate based on an operating system stored in the memory 632.
本申请实施例还提供一种计算机程序产品,包括计算机程序,计算机程序被处理器执行时实现的一种二进制翻译方法。An embodiment of the present application also provides a computer program product, including a computer program and a binary translation method implemented when the computer program is executed by a processor.
本领域技术人员在考虑说明书及实践这里公开的发明后,将容易想到本申请的其它实施方案。本申请旨在涵盖本申请的任何变型、用途或者适应性变化,这些变型、用途或者适应性变化遵循本申请的一般性原理并包括本申请未公开的本技术领域中的公知常识或惯用技术手段。说明书和实施例仅被视为示例性的,本申请的真正范围和精神由下面的权利要求指出。Those skilled in the art will readily appreciate other embodiments of the present application after considering the specification and practicing the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of the present application that follow the general principles of this application and include common knowledge or customary techniques in the art not disclosed herein. The description and examples are to be considered as exemplary only, and the true scope and spirit of the present application are indicated by the following claims.
应当理解的是,本申请并不局限于上面已经描述并在附图中示出的精确结构,并且可以在不脱离其范围进行各种修改和改变。本申请的范围仅由所附的权利要求来限制。It should be understood that the present application is not limited to the exact structures described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present application is limited only by the appended claims.
Claims (13)
Priority Applications (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202510293378.3A CN119806544B (en) | 2025-03-12 | 2025-03-12 | Binary translation method, binary translation device, electronic equipment and readable storage medium |
Applications Claiming Priority (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202510293378.3A CN119806544B (en) | 2025-03-12 | 2025-03-12 | Binary translation method, binary translation device, electronic equipment and readable storage medium |
Publications (2)
| Publication Number | Publication Date |
|---|---|
| CN119806544A CN119806544A (en) | 2025-04-11 |
| CN119806544B true CN119806544B (en) | 2025-08-15 |
Family
ID=95274951
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| CN202510293378.3A Active CN119806544B (en) | 2025-03-12 | 2025-03-12 | Binary translation method, binary translation device, electronic equipment and readable storage medium |
Country Status (1)
| Country | Link |
|---|---|
| CN (1) | CN119806544B (en) |
Families Citing this family (1)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN120821680B (en) * | 2025-09-18 | 2025-11-25 | 沐曦集成电路(南京)有限公司 | DMA-based data copying method, apparatus, device and storage medium |
Citations (1)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN104461925A (en) * | 2014-11-14 | 2015-03-25 | 浪潮(北京)电子信息产业有限公司 | Automatic correcting method and device for aligning storage device addresses |
Family Cites Families (5)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| EP1447742A1 (en) * | 2003-02-11 | 2004-08-18 | STMicroelectronics S.r.l. | Method and apparatus for translating instructions of an ARM-type processor into instructions for a LX-type processor |
| CN101299192B (en) * | 2008-06-18 | 2010-06-02 | 中国科学院计算技术研究所 | A processing method for unaligned memory access |
| US9996329B2 (en) * | 2016-02-16 | 2018-06-12 | Microsoft Technology Licensing, Llc | Translating atomic read-modify-write accesses |
| CN119248286B (en) * | 2024-12-03 | 2025-03-07 | 北京麟卓信息科技有限公司 | A cross-memory page difference compatible operation method based on memory access instruction reconstruction |
| CN119336392B (en) * | 2024-12-18 | 2025-04-11 | 龙芯中科技术股份有限公司 | Binary translation method, binary translation device, electronic equipment and readable storage medium |
-
2025
- 2025-03-12 CN CN202510293378.3A patent/CN119806544B/en active Active
Patent Citations (1)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN104461925A (en) * | 2014-11-14 | 2015-03-25 | 浪潮(北京)电子信息产业有限公司 | Automatic correcting method and device for aligning storage device addresses |
Also Published As
| Publication number | Publication date |
|---|---|
| CN119806544A (en) | 2025-04-11 |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| US20250265061A1 (en) | Systems and Methods for Performing Binary Translation | |
| US9411591B2 (en) | Run-time instrumentation sampling in transactional-execution mode | |
| US9400736B2 (en) | Transformation of a program-event-recording event into a run-time instrumentation event | |
| US9489285B2 (en) | Modifying run-time-instrumentation controls from a lesser-privileged state | |
| US9250903B2 (en) | Determining the status of run-time-instrumentation controls | |
| US20130246746A1 (en) | Run-time instrumentation directed sampling | |
| RU2769785C1 (en) | Cache-based trace playback breakpoints using tag field reserved bits | |
| WO2013136700A1 (en) | Run-time instrumentation reporting | |
| US20130246754A1 (en) | Run-time instrumentation indirect sampling by address | |
| US20130246772A1 (en) | Run-time instrumentation indirect sampling by instruction operation code | |
| CN119806544B (en) | Binary translation method, binary translation device, electronic equipment and readable storage medium | |
| US10169196B2 (en) | Enabling breakpoints on entire data structures | |
| US9395989B2 (en) | Run-time-instrumentation controls emit instruction | |
| CN119781831B (en) | Interrupt detection method, device, electronic equipment and readable storage medium |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| PB01 | Publication | ||
| PB01 | Publication | ||
| SE01 | Entry into force of request for substantive examination | ||
| SE01 | Entry into force of request for substantive examination | ||
| GR01 | Patent grant | ||
| GR01 | Patent grant |