Sathees Balya | 17d8eed | 2019-01-30 15:56:44 +0000 | [diff] [blame] | 1 | Library at ROM |
| 2 | ============== |
| 3 | |
Sathees Balya | 17d8eed | 2019-01-30 15:56:44 +0000 | [diff] [blame] | 4 | This document provides an overview of the "library at ROM" implementation in |
| 5 | Trusted Firmware-A (TF-A). |
| 6 | |
| 7 | Introduction |
| 8 | ~~~~~~~~~~~~ |
| 9 | |
| 10 | The "library at ROM" feature allows platforms to build a library of functions to |
| 11 | be placed in ROM. This reduces SRAM usage by utilising the available space in |
| 12 | ROM. The "library at ROM" contains a jump table with the list of functions that |
| 13 | are placed in ROM. The capabilities of the "library at ROM" are: |
| 14 | |
| 15 | 1. Functions can be from one or several libraries. |
| 16 | |
| 17 | 2. Functions can be patched after they have been programmed into ROM. |
| 18 | |
| 19 | 3. Platform-specific libraries can be placed in ROM. |
| 20 | |
| 21 | 4. Functions can be accessed by one or more BL images. |
| 22 | |
| 23 | Index file |
| 24 | ~~~~~~~~~~ |
| 25 | |
Paul Beesley | 814f8c0 | 2019-03-13 15:49:27 +0000 | [diff] [blame] | 26 | .. image:: ../resources/diagrams/romlib_design.png |
Sathees Balya | 17d8eed | 2019-01-30 15:56:44 +0000 | [diff] [blame] | 27 | :width: 600 |
| 28 | |
| 29 | Library at ROM is described by an index file with the list of functions to be |
| 30 | placed in ROM. The index file is platform specific and its format is: |
| 31 | |
| 32 | :: |
| 33 | |
| 34 | lib function [patch] |
| 35 | |
| 36 | lib -- Name of the library the function belongs to |
| 37 | function -- Name of the function to be placed in library at ROM |
| 38 | [patch] -- Option to patch the function |
| 39 | |
| 40 | It is also possible to insert reserved spaces in the list by using the keyword |
| 41 | "reserved" rather than the "lib" and "function" names as shown below: |
| 42 | |
| 43 | :: |
| 44 | |
Imre Kis | 2d137c5 | 2019-07-09 18:30:58 +0200 | [diff] [blame] | 45 | reserved |
Sathees Balya | 17d8eed | 2019-01-30 15:56:44 +0000 | [diff] [blame] | 46 | |
| 47 | The reserved spaces can be used to add more functions in the future without |
| 48 | affecting the order and location of functions already existing in the jump |
| 49 | table. Also, for additional flexibility and modularity, the index file can |
| 50 | include other index files. |
| 51 | |
| 52 | For an index file example, refer to ``lib/romlib/jmptbl.i``. |
| 53 | |
| 54 | Wrapper functions |
| 55 | ~~~~~~~~~~~~~~~~~ |
| 56 | |
Paul Beesley | 814f8c0 | 2019-03-13 15:49:27 +0000 | [diff] [blame] | 57 | .. image:: ../resources/diagrams/romlib_wrapper.png |
Sathees Balya | 17d8eed | 2019-01-30 15:56:44 +0000 | [diff] [blame] | 58 | :width: 600 |
| 59 | |
| 60 | When invoking a function of the "library at ROM", the calling sequence is as |
| 61 | follows: |
| 62 | |
| 63 | BL image --> wrapper function --> jump table entry --> library at ROM |
| 64 | |
| 65 | The index file is used to create a jump table which is placed in ROM. Then, the |
| 66 | wrappers refer to the jump table to call the "library at ROM" functions. The |
| 67 | wrappers essentially contain a branch instruction to the jump table entry |
| 68 | corresponding to the original function. Finally, the original function in the BL |
| 69 | image(s) is replaced with the wrapper function. |
| 70 | |
| 71 | The "library at ROM" contains a necessary init function that initialises the |
| 72 | global variables defined by the functions inside "library at ROM". |
| 73 | |
Jimmy Brisson | 1dfa271 | 2024-07-22 12:55:43 -0500 | [diff] [blame] | 74 | Wrapper functions are specified at the link stage of compilation and cannot |
| 75 | interpose uppon functions within the same translation unit. For example, if |
| 76 | function ``fn_a`` calls ``fn_b`` within translation unit ``functions.c`` and |
| 77 | the romlib jump table includes an entry for ``fn_b``, ``fn_a`` will include |
| 78 | a reference to ``fn_b``'s original program text instead of the wrapper. Thus |
| 79 | the jumptable author must take care to include public entry points into |
| 80 | translation units to avoid paying the program text cost twice, once in the |
| 81 | original executable and once in romlib. |
| 82 | |
Imre Kis | 2d137c5 | 2019-07-09 18:30:58 +0200 | [diff] [blame] | 83 | Script |
| 84 | ~~~~~~ |
Sathees Balya | 17d8eed | 2019-01-30 15:56:44 +0000 | [diff] [blame] | 85 | |
Manish V Badarkhe | acf72eb | 2024-04-18 10:13:36 +0100 | [diff] [blame] | 86 | There is a ``romlib_generator.py`` Python script that generates the necessary |
Imre Kis | 2d137c5 | 2019-07-09 18:30:58 +0200 | [diff] [blame] | 87 | files for the "library at ROM" to work. It implements multiple functions: |
Sathees Balya | 17d8eed | 2019-01-30 15:56:44 +0000 | [diff] [blame] | 88 | |
Manish V Badarkhe | acf72eb | 2024-04-18 10:13:36 +0100 | [diff] [blame] | 89 | 1. ``romlib_generator.py gentbl [args]`` - Generates the jump table by parsing |
Imre Kis | 2d137c5 | 2019-07-09 18:30:58 +0200 | [diff] [blame] | 90 | the index file. |
Sathees Balya | 17d8eed | 2019-01-30 15:56:44 +0000 | [diff] [blame] | 91 | |
Imre Kis | 2d137c5 | 2019-07-09 18:30:58 +0200 | [diff] [blame] | 92 | 2. ``romlib_generator.py genvar [args]`` - Generates the jump table global |
| 93 | variable (**not** the jump table itself) with the absolute address in ROM. |
| 94 | This global variable is, basically, a pointer to the jump table. |
Sathees Balya | 17d8eed | 2019-01-30 15:56:44 +0000 | [diff] [blame] | 95 | |
Imre Kis | 2d137c5 | 2019-07-09 18:30:58 +0200 | [diff] [blame] | 96 | 3. ``romlib_generator.py genwrappers [args]`` - Generates a wrapper function for |
| 97 | each entry in the index file except for the ones that contain the keyword |
Jimmy Brisson | 1dfa271 | 2024-07-22 12:55:43 -0500 | [diff] [blame] | 98 | ``patch``. The generated wrapper file is called ``wrappers.s``. |
Imre Kis | 2d137c5 | 2019-07-09 18:30:58 +0200 | [diff] [blame] | 99 | |
| 100 | 4. ``romlib_generator.py pre [args]`` - Preprocesses the index file which means |
| 101 | it resolves all the include commands in the file recursively. It can also |
| 102 | generate a dependency file of the included index files which can be directly |
| 103 | used in makefiles. |
| 104 | |
Manish V Badarkhe | acf72eb | 2024-04-18 10:13:36 +0100 | [diff] [blame] | 105 | Each ``romlib_generator.py`` function has its own manual which is accessible by |
Imre Kis | 2d137c5 | 2019-07-09 18:30:58 +0200 | [diff] [blame] | 106 | runing ``romlib_generator.py [function] --help``. |
| 107 | |
Manish V Badarkhe | acf72eb | 2024-04-18 10:13:36 +0100 | [diff] [blame] | 108 | ``romlib_generator.py`` requires Python 3 environment. |
Imre Kis | 2d137c5 | 2019-07-09 18:30:58 +0200 | [diff] [blame] | 109 | |
Sathees Balya | 17d8eed | 2019-01-30 15:56:44 +0000 | [diff] [blame] | 110 | |
| 111 | Patching of functions in library at ROM |
| 112 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 113 | |
Imre Kis | 2d137c5 | 2019-07-09 18:30:58 +0200 | [diff] [blame] | 114 | The ``romlib_generator.py genwrappers`` does not generate wrappers for the |
| 115 | entries in the index file that contain the keyword ``patch``. Thus, it allows |
| 116 | calling the function from the actual library by breaking the link to the |
| 117 | "library at ROM" version of this function. |
Sathees Balya | 17d8eed | 2019-01-30 15:56:44 +0000 | [diff] [blame] | 118 | |
| 119 | The calling sequence for a patched function is as follows: |
| 120 | |
| 121 | BL image --> function |
| 122 | |
Louis Mayencourt | bb2c538 | 2019-10-11 15:27:01 +0100 | [diff] [blame] | 123 | Memory impact |
| 124 | ~~~~~~~~~~~~~ |
| 125 | |
| 126 | Using library at ROM will modify the memory layout of the BL images: |
Paul Beesley | d2fcc4e | 2019-05-29 13:59:40 +0100 | [diff] [blame] | 127 | |
Louis Mayencourt | bb2c538 | 2019-10-11 15:27:01 +0100 | [diff] [blame] | 128 | - The ROM library needs a page aligned RAM section to hold the RW data. This |
Paul Beesley | d2fcc4e | 2019-05-29 13:59:40 +0100 | [diff] [blame] | 129 | section is defined by the ROMLIB_RW_BASE and ROMLIB_RW_END macros. |
| 130 | On Arm platforms a section of 1 page (0x1000) is allocated at the top of SRAM. |
| 131 | This will have for effect to shift down all the BL images by 1 page. |
| 132 | |
Louis Mayencourt | bb2c538 | 2019-10-11 15:27:01 +0100 | [diff] [blame] | 133 | - Depending on the functions moved to the ROM library, the size of the BL images |
Paul Beesley | d2fcc4e | 2019-05-29 13:59:40 +0100 | [diff] [blame] | 134 | will be reduced. |
| 135 | For example: moving MbedTLS function into the ROM library reduces BL1 and |
| 136 | BL2, but not BL31. |
| 137 | |
Louis Mayencourt | bb2c538 | 2019-10-11 15:27:01 +0100 | [diff] [blame] | 138 | - This change in BL images size can be taken into consideration to optimize the |
Paul Beesley | d2fcc4e | 2019-05-29 13:59:40 +0100 | [diff] [blame] | 139 | memory layout when defining the BLx_BASE macros. |
Louis Mayencourt | bb2c538 | 2019-10-11 15:27:01 +0100 | [diff] [blame] | 140 | |
Sathees Balya | 17d8eed | 2019-01-30 15:56:44 +0000 | [diff] [blame] | 141 | Build library at ROM |
| 142 | ~~~~~~~~~~~~~~~~~~~~~ |
| 143 | |
Paul Beesley | d2fcc4e | 2019-05-29 13:59:40 +0100 | [diff] [blame] | 144 | The environment variable ``CROSS_COMPILE`` must be set appropriately. Refer to |
| 145 | :ref:`Performing an Initial Build` for more information about setting this |
| 146 | variable. |
| 147 | |
John Tsichritzis | aec19d3 | 2019-03-08 16:54:13 +0000 | [diff] [blame] | 148 | In the below example the usage of ROMLIB together with mbed TLS is demonstrated |
| 149 | to showcase the benefits of library at ROM - it's not mandatory. |
Sathees Balya | 17d8eed | 2019-01-30 15:56:44 +0000 | [diff] [blame] | 150 | |
Paul Beesley | 493e349 | 2019-03-13 15:11:04 +0000 | [diff] [blame] | 151 | .. code:: shell |
Sathees Balya | 17d8eed | 2019-01-30 15:56:44 +0000 | [diff] [blame] | 152 | |
| 153 | make PLAT=fvp \ |
| 154 | MBEDTLS_DIR=</path/to/mbedtls/> \ |
| 155 | TRUSTED_BOARD_BOOT=1 GENERATE_COT=1 \ |
| 156 | ARM_ROTPK_LOCATION=devel_rsa \ |
| 157 | ROT_KEY=plat/arm/board/common/rotpk/arm_rotprivk_rsa.pem \ |
| 158 | BL33=</path/to/bl33.bin> \ |
| 159 | USE_ROMLIB=1 \ |
| 160 | all fip |
| 161 | |
Sathees Balya | 17d8eed | 2019-01-30 15:56:44 +0000 | [diff] [blame] | 162 | -------------- |
| 163 | |
| 164 | *Copyright (c) 2019, Arm Limited. All rights reserved.* |