Debian Codegraph @

Documentation/getting_started/cbmem.md

raw symbols SCIP
1 # CBMEM high table memory manager
2
3 ## Introduction
4
5 CBMEM (coreboot memory) is a dynamic memory infrastructure used in
6 coreboot to store runtime data structures, logs, and other information
7 that needs to persist across different boot stages, and even across warm
8 boots. CBMEM is crucial for maintaining state and information through
9 the coreboot boot process.
10
11 Its key responsibilities include:
12 - Providing a stable storage area for critical boot data
13 - Managing console logging
14 - Storing configuration tables for hand off to payloads
15 - Maintaining timestamps for performance analysis
16 - Preserving boot state during S3 suspend/resume cycles
17 - Storing data such as ACPI tables, SMBIOS tables and coreboot tables
18 which are used at runtime
19
20
21 ## Creation and Placement
22
23 CBMEM is initialized by coreboot during romstage, but is used mainly in
24 ramstage for storing any data that needs to be saved for more than a
25 short period of time.
26
27 For 32-bit builds, CBMEM is typically located at the highest usable DRAM
28 address below the 4GiB boundary. For 64-bit builds, while there is no
29 strict upper limit, it is advisable to follow the same guidelines to
30 prevent access or addressing issues. Regardless of the build type, the
31 CBMEM address must remain consistent between romstage and ramstage.
32
33 Each platform may need to implement its own method for determining the
34 `cbmem_top` address, as this can depend on specific hardware
35 configurations and memory layouts.
36
37
38 ## Architecture Overview
39
40 Each CBMEM region is identified by a unique ID, allowing different
41 components to store and retrieve their data during runtime.
42
43 Upon creating an entry, a block of memory of the requested size is
44 allocated from the reserved CBMEM region. This region typically persists
45 across warm reboots, making it useful for debugging and passing
46 information to payloads.
47
48 CBMEM is implemented as a specialized in-memory database (IMD) system
49 that grows downward from a defined upper memory limit. This means that
50 only the latest added entry may be removed.
51
52 ```text
53 High Memory
54 +----------------------+ <- cbmem_top
55 | Root Pointer |
56 |----------------------|
57 | Root Structure |
58 |----------------------|
59 | Entry 1 |
60 |----------------------|
61 | Entry 2 |
62 |----------------------|
63 | ... |
64 | (grows downward) |
65 +----------------------+
66 ```
67
68
69 ### Core Components
70
71 The CBMEM system consists of several key components:
72
73 1. **Root Pointer**: Located at the very top of CBMEM memory space,
74 contains a magic number and offset to the Root Structure
75
76 2. **Root Structure**: Contains metadata about the CBMEM region,
77 including:
78 - Entry alignment requirements
79 - Maximum number of entries
80 - Current number of entries
81 - Address of the lowest allocated memory
82
83 3. **Entries**: Individual allocations within CBMEM, identified by:
84 - 32-bit ID (often encoding ASCII characters for readability)
85 - Size information
86 - Magic validation value
87
88 4. **IMD Implementation**: The underlying memory management system
89 that handles allocation, deallocation, and entry management
90
91
92 ## Memory Layout and Initialization
93
94 CBMEM is positioned at the top of available system RAM, typically
95 just below the 4GiB boundary for 32-bit builds. This placement
96 ensures compatibility with legacy code while allowing CBMEM to
97 retain its contents across warm resets.
98
99
100 ### CBMEM Location Determination
101
102 Each platform must implement a `cbmem_top_chipset()` function
103 that returns the physical address where CBMEM should be located.
104 This address must be consistent across coreboot stages and is
105 determined based on:
106
107 - Available physical memory
108 - Architecture-specific constraints
109 - BIOS/chipset requirements
110
111
112 ### Initialization Process
113
114 CBMEM is initialized in a multi-step process:
115
116 1. **Early Initialization**: A small console buffer is created in during
117 bootblock and romstage
118
119 2. **RAM Initialization**: The full CBMEM area is established in
120 ramstage
121
122 3. **Normal Operation**:
123 - In a normal boot, CBMEM is created fresh
124 - Size and alignment values are specified
125 - Initial entries are allocated
126
127 4. **Recovery Operation**:
128 - During S3 resume, CBMEM structure is recovered from memory
129 - Content is validated using magic numbers and checksums
130 - All existing entries remain accessible
131
132
133 ## Entry Management
134
135 CBMEM entries are identified by a 32-bit ID, often encoding ASCII
136 characters to indicate their purpose (for example, "CNSL" for console).
137
138 ### Entry Creation
139
140 ```c
141 void *cbmem_add(u32 id, u64 size);
142 ```
143
144 This function:
145 - Searches for an existing entry with the given ID
146 - If found, returns the existing entry (size is ignored)
147 - If not found, allocates a new entry of the requested size
148 - Returns a pointer to the entry's data area
149
150
151 ### Entry Access
152
153 ```c
154 void *cbmem_find(u32 id);
155 ```
156
157 This function:
158 - Searches for an entry with the specified ID
159 - Returns a pointer to the entry's data area if found
160 - Returns NULL if the entry does not exist
161
162
163 ### Entry Removal
164
165 ```c
166 int cbmem_entry_remove(const struct cbmem_entry *entry);
167 ```
168
169 This function:
170 - Removes the specified entry if it was the last one added
171 - Returns 0 on success, negative value on failure
172 - Note: Due to the downward-growing design, only the most recently
173 added entry can be removed
174
175
176 ## CBMEM Console Implementation
177
178 The CBMEM console is a circular buffer used for capturing log messages
179 across all boot stages. It is one of the most widely used CBMEM
180 features. The size of this buffer is determined by the
181 `CONFIG_CBMEM_CONSOLE_SIZE` Kconfig option.
182
183
184 ### Console Structure
185
186 ```c
187 struct cbmem_console {
188 u32 size; // Size of the buffer
189 u32 cursor; // Current write position and flags
190 u8 body[]; // Actual data buffer
191 };
192 ```
193
194 Key features:
195 - The high bit of `cursor` indicates overflow condition
196 - Only the lower 28 bits of `cursor` are used as position
197 - Supports ring-buffer operation when full
198
199
200 ### Console Operation
201
202 1. **Initialization**: A console entry is created in CBMEM with ID
203 `CBMEM_ID_CONSOLE` (0x434f4e53 - 'CONS')
204
205 2. **Writing**: Log messages are written byte-by-byte using
206 `cbmemc_tx_byte()` which:
207 - Adds data to the current cursor position
208 - Advances the cursor
209 - Sets the overflow flag when wrapping around
210
211 3. **Stage Transition**: When transitioning between boot stages:
212 - Pre-RAM console contents are copied to the main CBMEM console
213 - Any overflow condition is preserved and noted
214 - The process ensures no log messages are lost
215
216
217 ## Common CBMEM Entry Types
218
219 CBMEM contains various data structures used by different coreboot
220 components:
221
222 - **Console**: Log messages from all boot stages (`CBMEM_ID_CONSOLE`)
223 - **Timestamps**: Performance metrics (`CBMEM_ID_TIMESTAMP`)
224 - **ACPI Tables**: ACPI data for OS handoff (`CBMEM_ID_ACPI`)
225 - **coreboot Tables**: System information (`CBMEM_ID_CBTABLE`)
226 - **Memory Information**: RAM configuration (`CBMEM_ID_MEMINFO`)
227 - **Stage Cache**: Code/data for faster S3 resume
228 - **ROM/CBFS Cache**: Cached ROM content
229 - **Vendor-specific**: Platform-dependent structures
230
231 A complete list of IDs is defined in
232 `src/commonlib/bsd/include/commonlib/bsd/cbmem_id.h`.
233
234
235 ## Integration with Boot Stages
236
237 CBMEM interacts differently with each coreboot boot stage.
238
239
240 ### Bootblock/Romstage
241
242 - Uses cache-as-RAM for temporary console storage
243 - Limited CBMEM functionality before RAM initialization
244 - Sets up initial timestamp entries
245
246
247 ### Ramstage
248
249 - Full CBMEM initialization or recovery
250 - All entries become accessible
251 - Most coreboot subsystems interact with CBMEM
252 - Console logging is fully operational
253
254
255 ### Payload Handoff
256
257 - CBMEM contents are preserved when transferring to the payload
258 - Entries are made available via coreboot tables
259 - Common payloads (SeaBIOS, GRUB, etc.) can access CBMEM data
260
261
262 ## Platform-Specific Considerations
263
264 Different platforms have unique requirements for CBMEM implementation.
265
266
267 ### x86 Platforms
268
269 - CBMEM typically located just below 4GiB
270 - Often integrates with ACPI resume and SMM operations
271 - May need to accommodate memory reserved by legacy components
272
273
274 ### ARM/RISC-V Platforms
275
276 - More flexibility in CBMEM placement
277 - Must coordinate with platform-specific memory controllers
278 - Memory topology can vary significantly between implementations
279
280
281 ## CBMEM Hooks
282
283 CBMEM provides a hook mechanism to allow subsystems to perform
284 initialization or recovery operations when CBMEM becomes available:
285
286 ```c
287 CBMEM_CREATION_HOOK(hook_function); // First-time creation only
288 CBMEM_READY_HOOK(hook_function); // Any CBMEM initialization
289 CBMEM_READY_HOOK_EARLY(hook_function); // Early CBMEM initialization
290 ```
291
292 These macros register functions to be called when CBMEM is initialized,
293 allowing components to set up their CBMEM entries at the appropriate time.
294
295
296 ## Debugging and Utilities
297
298 ### CBMEM Utility
299
300 The `cbmem` utility provides direct access to CBMEM contents on a
301 running system. It needs to be built from the coreboot source tree using
302 `make -C util/cbmem`. Common uses include:
303
304 - Listing all CBMEM entries (`cbmem -l`)
305 - Viewing console logs (`cbmem -c`)
306 - Analyzing timestamps (`cbmem -t`)
307 - Extracting specific entries by ID (`cbmem -e <ID>`)
308
309
310 ### Debugging Techniques
311
312 - CBMEM console contents can be dumped to UART for debugging if serial
313 output is enabled.
314 - The console overflow flag (`cbmem -c` output) helps identify if logs
315 were truncated.
316 - Size validation within CBMEM helps detect potential memory corruption.
317 - Magic numbers provide integrity validation for CBMEM structures.
318 - Enabling `CONFIG_CBMEM_CHECKS` in Kconfig adds extra sanity checks
319 that can help catch issues during development.
320
321
322 ## Limitations
323
324 While CBMEM is a powerful tool, it has some inherent limitations:
325
326 - **Downward Growth**: The stack-like allocation (growing downwards)
327 means that only the most recently added entry can be removed. This
328 prevents fragmentation but limits flexibility in freeing memory.
329 - **Fixed Size**: Once CBMEM is initialized in ramstage, its total size
330 and top address (`cbmem_top`) are fixed. Entries cannot be resized
331 after allocation.
332 - **Platform Complexity**: Determining the correct `cbmem_top` can be
333 complex on some platforms due to varying memory maps and reserved
334 regions.
335
336
337 ## Best Practices for Developers
338
339 When working with the CBMEM subsystem:
340
341 1. **Alignment**: Always respect the alignment requirements of the
342 CBMEM tier (`IMD_ROOT` vs `IMD_SMALL`) you are using.
343
344 2. **ID Selection**: Use unique, meaningful IDs for new entries,
345 preferably encoding ASCII characters for readability (see
346 `cbmem_id.h`).
347
348 3. **Size Estimation**: Allocate sufficient space initially, as
349 entries cannot be resized.
350
351 4. **Memory Conservation**: Be mindful of limited memory resources,
352 especially on constrained platforms. Avoid storing excessively large
353 data structures in CBMEM unless necessary.
354
355 5. **Persistence**: Remember that CBMEM contents persist across
356 warm reboots (like S3 resume) but not across full system resets
357 (cold boots).
358
359 6. **Entry Ordering**: Consider that only the most recently added
360 entry can be removed, which might influence your allocation strategy
361 if you anticipate needing to free space.