Method for jailhouse implementation of bare-metal program using virtual serial port
By configuring shared memory and reimplementing the print function in Jailhouse, bare-metal programs can implement virtual serial port functionality without triggering VM exit, solving the problem of frequent switching of virtual serial ports in Jailhouse and improving real-time performance and efficiency.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- KYLIN CORP
- Filing Date
- 2026-05-11
- Publication Date
- 2026-06-09
Smart Images

Figure CN122173206A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of virtual serial port technology, and more specifically, to a method for implementing bare-metal programs to use virtual serial ports in Jailhouse. Background Technology
[0002] As industrial control, vehicle systems, and critical infrastructure applications increasingly demand higher levels of real-time performance, reliability, and security isolation, traditional virtualization technologies are showing limitations in terms of system complexity and operational overhead. Jailhousee is a lightweight virtualization technology designed for industrial applications. It introduces an asymmetric multiprocessing mechanism onto existing Linux systems, dividing the physical system into multiple independent running units (cells). Upon system startup, the Root Cell, used for system initialization and resource management, is run first. One or more Non-Root Cells can then be created on top of this, each running an independent operating system and allocated dedicated processor, memory, and peripheral resources. This enables parallel operation of multiple operating systems and strict isolation of hardware resources to meet the requirements of high reliability and deterministic operation.
[0003] On some hardware platforms running Jailhousee, physical serial port resources are limited, making it difficult to meet the simultaneous debugging and log output needs of multiple Non-Root Cells (such as bare-metal programs or RTOS). To address this, Jailhousee provides a virtual serial port mechanism for use by bare-metal programs running in Non-Root Cells. Existing virtual serial port implementations rely on executing hvc (ARM Hypervisor Call) instructions in the Non-Root Cell to trap serial port output requests into Jailhousee's hypervisor for processing. This process triggers a virtual machine exit, causing frequent control flow switching between the Non-Root Cell, hypervisor, and Root Cell. This results in a long path, significant processing overhead, and is unsuitable for applications with high real-time requirements. Summary of the Invention
[0004] To address the aforementioned technical issues, this invention proposes a method for implementing virtual serial ports in bare-metal programs using Jailhouse. This method achieves virtual serial port functionality without triggering VM exit. While maintaining the use of the virtual serial port interface in the Non-Root Cell, this method avoids entering the hypervisor's processing flow with each character output, thereby significantly shortening the data transmission path, reducing system overhead, and improving the real-time performance and overall operating efficiency of the virtual serial port.
[0005] This invention provides a method for Jailhouse to implement bare-metal programs using virtual serial ports, comprising the following steps: Step S1: Obtain the address of the shared memory of the virtual serial port; Step S2: Configure the shared memory address of the virtual serial port in Jailhouse; Step S3: Reimplement the print function in Jailhouse to write the output characters of the bare-metal program to the shared memory of the virtual serial port; Step S4: Recompile the Jailhouse code to generate a bare-metal program image file; start the bare-metal program based on the bare-metal program image file, and write the output of the bare-metal program to the shared memory of the virtual serial port through the print function.
[0006] Furthermore, step S1 includes the following steps: Step S11: Compile the Jailhouse code to generate a binary file, and analyze the binary file to obtain the offset of the shared memory of the virtual serial port relative to the firmware start address of Jailhouse; Step S12: Calculate the address of the shared memory of the virtual serial port based on the offset of the shared memory of the virtual serial port relative to the firmware start address of Jailhouse and the physical start address of the hypervisor.
[0007] Furthermore, in step S2, the size of the shared memory mapping of the virtual serial port is first determined, and then the mapped memory is added to the configuration file inmate.c of the Non-Root Cell in Jailhouse.
[0008] Furthermore, in step S2, the size of the shared memory mapping of the virtual serial port is at least 2056 bytes.
[0009] Furthermore, step S3 includes the following steps: Step S31: Define the shared memory structure for the virtual serial port in Jailhouse's inmate library; Step S32: Declare a global static variable for the shared memory structure of the virtual serial port, and map the address of the global static variable to the address of the shared memory of the virtual serial port; Step S33: Set the print function for the global static variable so that the output characters of the bare-metal program can be written to the shared memory of the virtual serial port through the print function.
[0010] Furthermore, the shared memory structure of the virtual serial port includes flag variables and a character buffer.
[0011] Furthermore, the flag variables include control bits and an offset pointer.
[0012] Furthermore, the control bit is set to busy, the offset pointer to tail, and the character buffer to content.
[0013] Furthermore, the global static variable is `console`, and the corresponding print function is `console_write`.
[0014] Furthermore, the bare-metal program's image file is gic-demo.bin.
[0015] The beneficial effects of this invention are as follows: This invention provides a method for implementing a virtual serial port without triggering a virtual machine exit. By configuring the physical address of the shared memory to a non-root unit, the bare-metal program can directly write to the shared buffer, and memory barriers are used to ensure data visibility, thereby achieving high real-time character output. Attached Figure Description
[0016] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0017] Figure 1 This is a flowchart of the present invention. Detailed Implementation
[0018] The technical solutions of the present invention will be clearly and completely described below with reference to the embodiments of the present invention. Many specific details are set forth in the following description to provide a thorough understanding of the present invention; however, the present invention may also be implemented in other ways different from those described herein. Those skilled in the art can make similar extensions without departing from the spirit of the present invention. Therefore, the present invention is not limited to the specific embodiments disclosed below.
[0019] This invention proposes a method for implementing virtual serial ports in bare-metal programs using Jailhouse, which achieves virtual serial port functionality without triggering VM exit. While maintaining the use of the virtual serial port interface in the Non-Root Cell, this method avoids entering the hypervisor's processing flow with each character output, thereby significantly shortening the data transmission path, reducing system overhead, and improving the real-time performance and overall operating efficiency of the virtual serial port.
[0020] Jailhousee's virtual serial port mechanism works by using HVC instructions to trap characters output by the bare-metal program into the hypervisor. The hypervisor then writes these characters to a shared memory block. The driver can then read this shared memory to print the characters output by the bare-metal program. Since the address of this shared memory is fixed, the address must be obtained before use and configured in the bare-metal program's configuration file. After reimplementing the print function in Jailhousee's inmate library, characters output by the bare-metal program can be directly written to this shared memory, and the driver can also access the data in this shared memory for output when needed.
[0021] Example 1: The present invention, Jailhouse, provides a method for implementing a bare-metal program to use a virtual serial port, comprising the following steps: Step S1: Obtain the address of the shared memory of the virtual serial port; Step S2: Configure the shared memory address of the virtual serial port in Jailhouse; Step S3: Reimplement the print function in Jailhouse to write the output characters of the bare-metal program to the shared memory of the virtual serial port; Step S4: Recompile the Jailhouse code to generate a bare-metal program image file; start the bare-metal program based on the bare-metal program image file, and write the output of the bare-metal program to the shared memory of the virtual serial port through the print function.
[0022] Example 2: This embodiment provides a specific implementation step: Step S0: Prepare a Hanwei e2000q development board, install the Ubuntu 20.04 system, install the Jailhouse code, and prepare the configuration file main.c for starting the Jailhouse Root Cell and the configuration file inmate.c for starting the Non-Root Cell.
[0023] Step S1 (corresponding to) Figure 1 Step A): Obtain the shared memory address of the virtual serial port; Specifically, step S1 includes the following steps: Step S11: Compile the Jailhouse code to generate a binary file (Jailhouse.bin symbol table), and analyze the binary file to obtain the offset of the shared memory of the virtual serial port relative to the starting address of Jailhouse. Step S12: Calculate the address of the shared memory of the virtual serial port based on the offset of the shared memory of the virtual serial port relative to the starting address of the Jailhouse and the starting physical address of the hypervisor in the RootCell configuration file main.c. The formula for calculating the address of the shared memory of the virtual serial port is: virt_console_phys_addr=hypervisor.mem+offset Where virt_console_phys_addr represents the address of the shared memory of the virtual serial port, hypervisor.mem represents the starting physical address of the hypervisor, and offset represents the offset.
[0024] The above calculations determine the physical memory region (shared memory) shared between the Root Cell and the Non-Root Cell, which will serve as a data exchange buffer for the virtual serial port.
[0025] Step S2 (corresponding to) Figure 1 Step B): Configure the shared memory of the virtual serial port in the Non-Root Cell of the Jailhouse based on the address of the shared memory of the virtual serial port. To avoid the performance overhead caused by the traditional virtual serial port's reliance on the VM-exit mechanism, this embodiment directly maps the shared memory to the Non-Root Cell, enabling it to complete data writing locally.
[0026] Specifically, the shared memory is used to construct a virtual serial port buffer. Its structure includes a character buffer and two status flag variables, busy and tail. The character buffer occupies 2048 bytes; tail is a write pointer used to identify the current write position, occupying 8 bytes; and busy is a write status flag used for synchronous access, occupying 8 bytes. Therefore, the total size of the shared memory to be mapped is 2056 bytes.
[0027] Add the following memory region to the Jailhouse's Non-Root Cell configuration file inmate.c: { .phys_start = addr, .virt_start = addr, .size = 0x808, / / 2056 bytes .flags = JAILHOUSE_MEM_WRITE | JAILHOUSE_MEM_ROOTSHARED, }, Wherein: JAILHOUSE_MEM_ROOTSHARED indicates that this memory region can be shared by the Root Cell and Non-Root Cell. Physical address consistency mapping (phys = virt) avoids additional address translation overhead.
[0028] Step S3 (corresponding to) Figure 1 Step C): Reimplement the print function in Jailhouse to write the output characters of the bare-metal program to the shared memory of the virtual serial port.
[0029] Step S3 includes the following steps: Step S31, define the shared memory structure: Define the shared memory structure for the virtual serial port in the Jailhouse inmate library; the code for the structure is as follows: struct Jailhouse_virt_console { unsigned int busy; unsigned int tail; char content
[2048] ; }; Here, content represents the character buffer.
[0030] Step S32, Mapping shared memory: Declare a global static variable console for the shared memory structure of the virtual serial port, and map the address of the global static variable console to the address of the shared memory of the virtual serial port (the address obtained in step S12); thus, the bare-metal program's access to console is a direct access to the shared memory.
[0031] Step S33, implement a character output mechanism without VM-exit: set a print function for the global static variable. The print function is: console_write. Through the print function and the global static variable, the output characters of the bare-metal program can be written to the shared memory of the virtual serial port, and visibility is guaranteed through a memory barrier. The sample code is as follows: console.busy = true; / / Set the busy flag to true to indicate that shared memory is currently being written to; memory_barrier(); / / Executes a memory barrier instruction to ensure that the reader cannot access the relevant data before the data is written, thereby ensuring the integrity and consistency of the read results.
[0032] while ( msg) { / / Write character by character to the shared memory buffer console.content[console.tail % sizeof(console.content)] = msg++; / / Calculate the index of the buffer where the current character should be written, and write the current character. console.tail++; / / A new character was successfully written, so the tail pointer is incremented by 1. } memory_barrier(); / / First, use a memory barrier to ensure that all updates to the content buffer and tail have been fully committed and are visible to the outside world. console.busy = false; / / Indicates that the write operation has ended and the data in shared memory is in a stable and readable state. Step S4: Recompile the Jailhouse code to generate an image file (e.g., gic-demo.bin) of the bare-metal program corresponding to the Non-Root Cell. Start the bare-metal program based on the image file and write its output to the shared memory of the virtual serial port using a print function. The Jailhouse hypervisor reads data from the shared memory and outputs it externally through the virtual serial port (not shown in the figure).
[0033] Step S5 (corresponding to) Figure 1 Step E): Verify the output function of the virtual serial port.
[0034] The steps of the entire process are as follows: 1. Run the command `Jailhouse enable main.cell` to start kvisor. 2. Run the command `Jailhouse cell create inmate.cell` to create a Non-Root Cell. 3. Run the command `Jailhouse cell load 1 gic-demo.bin` to load the bare-metal program image file. 4. Run the command "Jailhouse cell start 1" to start the bare-metal program. 5. Running the Jailhouse console -f command allows you to view the output of the bare-metal program from the memory of the virtual serial port, confirming whether it can be printed correctly from the virtual serial port.
[0035] The main.cell is compiled from the root cell's configuration file main.c, the inmate.cell is compiled from the non-root cell's configuration file inmate.c, and gic-demo.bin is the bare-metal program image file that comes with Jailhouse.
[0036] Follow the steps in this embodiment and run the command `Jailhouse console -f`. You will see the following logs from the bare-metal program `gic-demo`: Timer fired, jitter: 205064 ns, min: 203306 ns, max: 2318016 ns Timer fired, jitter: 287838 ns, min: 203306 ns, max: 2318016 ns Timer fired, jitter: 274145 ns, min: 203306 ns, max: 2318016 ns Timer fired, jitter: 373999 ns, min: 203306 ns, max: 2318016 ns Timer fired, jitter: 256612 ns, min: 203306 ns, max: 2318016 ns Timer fired, jitter: 242370 ns, min: 203306 ns, max: 2318016 ns Timer fired, jitter: 234274 ns, min: 203306 ns, max: 2318016 ns Timer fired, jitter: 255532 ns, min: 203306 ns, max: 2318016 ns Timer fired, jitter: 440387 ns, min: 203306 ns, max: 2318016 ns Timer fired, jitter: 349209 ns, min: 203306 ns, max: 2318016 ns Timer fired, jitter: 256516 ns, min: 203306 ns, max: 2318016 ns Timer fired, jitter: 425193 ns, min: 203306 ns, max: 2318016 ns Timer fired, jitter: 296274 ns, min: 203306 ns, max: 2318016 ns Timer fired, jitter: 210645 ns, min: 203306 ns, max: 2318016 ns Timer fired, jitter: 166322 ns, min: 166322 ns, max: 2318016 ns Timer fired, jitter: 185661 ns, min: 166322 ns, max: 2318016 ns Timer fired, jitter: 201612 ns, min: 166322 ns, max: 2318016 ns Timer fired, jitter: 194403 ns, min: 166322 ns, max: 2318016 ns Terminology Explanation: Root Cell: The Root Cell is the first running unit after the system boots up. The Root Cell typically loads and runs the Linux operating system, handling hardware initialization, unified management of resources such as CPU and memory, and the creation and configuration of other subsequent cells.
[0037] Non-Root Cell: An independent running unit created by Jailhouse after the Root Cell is initialized. A Non-Root Cell can run an independent guest operating system or bare-metal program, using only the processor cores, memory regions, and peripheral resources statically allocated at creation. It does not depend on the Linux kernel during operation and does not participate in system-level resource management.
[0038] Bare-metal program: A program that runs in a non-root cell and does not rely on the support of a general operating system.
[0039] hvc: refers to the privileged instruction used to trigger processing at the virtualization management layer. In Jailhouse, when a program in a Non-Root Cell executes the hvc instruction, the processor generates a trap event, causing the execution flow to switch from the current cell to the Jailhouse Hypervisor. After the Hypervisor processes the request, it returns to the original execution environment.
[0040] VM-exit: During Jailhouse virtualization, when a Non-Root Cell executes a specific instruction or a specific exception occurs, the processor transfers control from the current cell to the Hypervisor.
[0041] Obviously, the described embodiments are only some, not all, of the embodiments of the present invention. All other embodiments obtained by those skilled in the art based on the embodiments of the present invention without inventive effort are within the scope of protection of the present invention.
Claims
1. A method for implementing bare-metal programs using virtual serial ports using Jailhouse, characterized in that... Includes the following steps: Step S1: Obtain the address of the shared memory of the virtual serial port; Step S2: Configure the shared memory address of the virtual serial port in Jailhouse; Step S3: Reimplement the print function in Jailhouse to write the output characters of the bare-metal program to the shared memory of the virtual serial port; Step S4: Recompile the Jailhouse code to generate a bare-metal program image file; start the bare-metal program based on the bare-metal program image file, and write the output of the bare-metal program to the shared memory of the virtual serial port through the print function.
2. The method for implementing a bare-metal program using a virtual serial port using Jailhouse according to claim 1, characterized in that, Step S1 includes the following steps: Step S11: Compile the Jailhouse code to generate a binary file, and analyze the binary file to obtain the offset of the shared memory of the virtual serial port relative to the firmware start address of Jailhouse; Step S12: Calculate the address of the shared memory of the virtual serial port based on the offset of the shared memory of the virtual serial port relative to the firmware start address of Jailhouse and the physical start address of the hypervisor.
3. The method for implementing a bare-metal program using a virtual serial port using Jailhouse according to claim 1, characterized in that, In step S2, first determine the size of the shared memory mapping for the virtual serial port, and then add the mapped memory to the configuration file inmate.c of the Non-Root Cell in Jailhouse.
4. The method for implementing a bare-metal program using a virtual serial port using Jailhouse according to claim 2, characterized in that, In step S2, the size of the shared memory mapping of the virtual serial port is at least 2056 bytes.
5. The method for implementing a bare-metal program using a virtual serial port using Jailhouse according to claim 1, characterized in that, Step S3 includes the following steps: Step S31: Define the shared memory structure for the virtual serial port in Jailhouse's inmate library; Step S32: Declare a global static variable for the shared memory structure of the virtual serial port, and map the address of the global static variable to the address of the shared memory of the virtual serial port; Step S33: Set the print function for the global static variable so that the output characters of the bare-metal program can be written to the shared memory of the virtual serial port through the print function.
6. The method for implementing a bare-metal program using a virtual serial port using Jailhouse according to claim 5, characterized in that, The shared memory structure of a virtual serial port includes a flag variable and a character buffer.
7. The method for implementing a bare-metal program using a virtual serial port using Jailhouse according to claim 6, characterized in that, Flag variables include control bits and an offset pointer.
8. The method for implementing a bare-metal program using a virtual serial port using Jailhouse according to claim 7, characterized in that, The control bit is set to busy, the offset pointer is set to tail, and the character buffer is set to content.
9. The method for implementing a bare-metal program using a virtual serial port using Jailhouse according to claim 5, characterized in that, The global static variable is `console`, and the corresponding print function is `console_write`.
10. The method for implementing a bare-metal program using a virtual serial port using Jailhouse according to claim 1, characterized in that, The image file for the bare-metal program is gic-demo.bin.