Debian Codegraph @

Documentation/lib/rmodules.md

raw symbols SCIP
1 # Relocatable Modules (rmodules)
2
3 Relocatable modules are currently only used on x86. Relocatable
4 modules are executables. Exectuables which can be executed anywhere in
5 memory. Anywhere means that the module does not need to be executed
6 at a defined memory address which is known at build/link time. For
7 coreboot stages like bootblock and romstage it is known at build
8 time at which addresses they are executed. For some exectuables it
9 is however not known at which specific address they are executed in
10 runtime (for example postcar and ramstage). Relocateable modules
11 usually allocate the space for the modules just before they are
12 supposed to be executed. After enough space is allocated, CBMEM will
13 return the location of the allocated space. Now the relocation can be
14 done by fixing up all relocation entries in the relocatable module
15 based on the location of the binary (which was returned by CBMEM
16 at runtime).
17
18 ## Implementation Details
19
20 ### Build Time
21
22 At build time the rmodtool (util/cbfstool/rmodtool.c) is used to
23 create relocatable modules. The rmodtool basically takes an ELF
24 file as an input and writes an ELF as output. It basically does
25 a simple conversion from one ELF file to another slighty changed
26 ELF file. First the tool makes sure that the ELF file fits a few
27 requirements. For example there can only be one segment (loadable
28 program header) in the input ELF file. After that it goes through
29 the ELF relocation table and takes any entry that applies to the one
30 segment we want to load at runtime. The rmodtool will then write all
31 these relocation entires in a new ELF section called ".reloc". After
32 that the ELF relocation table will be cleared.
33
34 One can split the rmodules in two different kinds:
35 1. coreboot stages (postcar, ramstage)
36 2. simple binaries (smm, smmstub, sipi\_vector)
37
38 There is one important difference in how they are handled:
39 The simple binaries are compiled into rmodules the same as coreboot
40 stages are, but the simple binaries are always directly linked to a
41 stage. Since rmodules are ELF files as well, we can easily link them
42 to the stages in which we need them (usually postcar or ramstage).
43 So they are not really separate modules anymore, but still retain
44 the ability to accept rmodule\_parameters.
45 Since the simple binaries are usually very small, linking them directly
46 into the stage (e.g. ramstage or postcar) avoids having to fetch them
47 from CBFS and running all that code to fetch a few hundred bytes of
48 code. So the build system handles them as follows:
49 1. create rmodule (which is an ELF file) from source files
50 2. remove all the ELF headers and sections that are not loadable using
51 `objcopy -O binary`
52 3. from this, create an object file, which usually has the self invented
53 .manual file extension, which can be linked to the appropriate stage
54 4. add the generated .manual file as "source" file to the stage we want
55 to link it to
56
57 In the end the ELF files will have three different ELF sections,
58 which are all created by the rmodtool.
59 1. relocation header (.header)
60 2. program (.program)
61 3. relocation entries (.relocs)
62
63 ### Runtime
64
65 Either rmodule\_load (lib/rmodule.c) is used directly or through the
66 rmodule\_stage\_load (lib/rmodule.c) wrapper. It is used to load the
67 stages (postcar and ramstage) or small programs like (sipi\_vector,
68 smm, smmstub) into memory before jumping to them. In the case of a
69 coreboot stage, CBMEM is used to allocate space for the stage in memory
70 via the rmodule\_cbfs\_allocater (lib/rmodule.c). At this point the
71 location of the stage in memory is known and all relocation (address
72 fixups) need to be done now. This is basically just a simple loop that
73 goes through each relocation entry. Each relocation entry is just an
74 address pointing to a location that needs relocation. The relocation
75 itself is just a simple addition, that adds an offset from where the
76 image was "supposed" to be at link time, to where it is now relocated.
77
78 ### module\_parameters
79
80 module\_parameters is a section inside the rmodule ELF file. Its
81 basically a way to pass runtime information to an rmodule
82 before jumping to it. The caller will use rmodule\_parameters()
83 (lib/rmodule.c) to get the runtime address of the module\_parameters
84 and the callee (the rmodule itself) usually appends the section to
85 specific types via compiler attributes. For example:
86 ```
87 static const
88 volatile __attribute((aligned(4), __section__(".module_parameters")))
89 struct smm_runtime smm_runtime;
90 ```
91
92 ## x86 why rmodules
93 //TODO
94 x86: postcar and ramstage cannot conflict with payload regarding
95 memory placement. Therefore payload location is usually fixed and
96 postcar/ramstage can be placed at a location in memory that is
97 figured out at runtime.