[init] Show startup and shutdown function names in debug messages
[ipxe.git] / src / arch / x86 / image / initrd.c
1 /*
2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <errno.h>
27 #include <initrd.h>
28 #include <ipxe/image.h>
29 #include <ipxe/uaccess.h>
30 #include <ipxe/init.h>
31 #include <ipxe/memblock.h>
32
33 /** @file
34 *
35 * Initial ramdisk (initrd) reshuffling
36 *
37 */
38
39 /** Maximum address available for initrd */
40 userptr_t initrd_top;
41
42 /** Minimum address available for initrd */
43 userptr_t initrd_bottom;
44
45 /**
46 * Squash initrds as high as possible in memory
47 *
48 * @v top Highest possible address
49 * @ret used Lowest address used by initrds
50 */
51 static userptr_t initrd_squash_high ( userptr_t top ) {
52 userptr_t current = top;
53 struct image *initrd;
54 struct image *highest;
55 size_t len;
56
57 /* Squash up any initrds already within or below the region */
58 while ( 1 ) {
59
60 /* Find the highest image not yet in its final position */
61 highest = NULL;
62 for_each_image ( initrd ) {
63 if ( ( userptr_sub ( initrd->data, current ) < 0 ) &&
64 ( ( highest == NULL ) ||
65 ( userptr_sub ( initrd->data,
66 highest->data ) > 0 ) ) ) {
67 highest = initrd;
68 }
69 }
70 if ( ! highest )
71 break;
72
73 /* Move this image to its final position */
74 len = ( ( highest->len + INITRD_ALIGN - 1 ) &
75 ~( INITRD_ALIGN - 1 ) );
76 current = userptr_sub ( current, len );
77 DBGC ( &images, "INITRD squashing %s [%#08lx,%#08lx)->"
78 "[%#08lx,%#08lx)\n", highest->name,
79 user_to_phys ( highest->data, 0 ),
80 user_to_phys ( highest->data, highest->len ),
81 user_to_phys ( current, 0 ),
82 user_to_phys ( current, highest->len ) );
83 memmove_user ( current, 0, highest->data, 0, highest->len );
84 highest->data = current;
85 }
86
87 /* Copy any remaining initrds (e.g. embedded images) to the region */
88 for_each_image ( initrd ) {
89 if ( userptr_sub ( initrd->data, top ) >= 0 ) {
90 len = ( ( initrd->len + INITRD_ALIGN - 1 ) &
91 ~( INITRD_ALIGN - 1 ) );
92 current = userptr_sub ( current, len );
93 DBGC ( &images, "INITRD copying %s [%#08lx,%#08lx)->"
94 "[%#08lx,%#08lx)\n", initrd->name,
95 user_to_phys ( initrd->data, 0 ),
96 user_to_phys ( initrd->data, initrd->len ),
97 user_to_phys ( current, 0 ),
98 user_to_phys ( current, initrd->len ) );
99 memcpy_user ( current, 0, initrd->data, 0,
100 initrd->len );
101 initrd->data = current;
102 }
103 }
104
105 return current;
106 }
107
108 /**
109 * Swap position of two adjacent initrds
110 *
111 * @v low Lower initrd
112 * @v high Higher initrd
113 * @v free Free space
114 * @v free_len Length of free space
115 */
116 static void initrd_swap ( struct image *low, struct image *high,
117 userptr_t free, size_t free_len ) {
118 size_t len = 0;
119 size_t frag_len;
120 size_t new_len;
121
122 DBGC ( &images, "INITRD swapping %s [%#08lx,%#08lx)<->[%#08lx,%#08lx) "
123 "%s\n", low->name, user_to_phys ( low->data, 0 ),
124 user_to_phys ( low->data, low->len ),
125 user_to_phys ( high->data, 0 ),
126 user_to_phys ( high->data, high->len ), high->name );
127
128 /* Round down length of free space */
129 free_len &= ~( INITRD_ALIGN - 1 );
130 assert ( free_len > 0 );
131
132 /* Swap image data */
133 while ( len < high->len ) {
134
135 /* Calculate maximum fragment length */
136 frag_len = ( high->len - len );
137 if ( frag_len > free_len )
138 frag_len = free_len;
139 new_len = ( ( len + frag_len + INITRD_ALIGN - 1 ) &
140 ~( INITRD_ALIGN - 1 ) );
141
142 /* Swap fragments */
143 memcpy_user ( free, 0, high->data, len, frag_len );
144 memmove_user ( low->data, new_len, low->data, len, low->len );
145 memcpy_user ( low->data, len, free, 0, frag_len );
146 len = new_len;
147 }
148
149 /* Adjust data pointers */
150 high->data = low->data;
151 low->data = userptr_add ( low->data, len );
152 }
153
154 /**
155 * Swap position of any two adjacent initrds not currently in the correct order
156 *
157 * @v free Free space
158 * @v free_len Length of free space
159 * @ret swapped A pair of initrds was swapped
160 */
161 static int initrd_swap_any ( userptr_t free, size_t free_len ) {
162 struct image *low;
163 struct image *high;
164 size_t padded_len;
165 userptr_t adjacent;
166
167 /* Find any pair of initrds that can be swapped */
168 for_each_image ( low ) {
169
170 /* Calculate location of adjacent image (if any) */
171 padded_len = ( ( low->len + INITRD_ALIGN - 1 ) &
172 ~( INITRD_ALIGN - 1 ) );
173 adjacent = userptr_add ( low->data, padded_len );
174
175 /* Search for adjacent image */
176 for_each_image ( high ) {
177
178 /* If we have found the adjacent image, swap and exit */
179 if ( high->data == adjacent ) {
180 initrd_swap ( low, high, free, free_len );
181 return 1;
182 }
183
184 /* Stop search if all remaining potential
185 * adjacent images are already in the correct
186 * order.
187 */
188 if ( high == low )
189 break;
190 }
191 }
192
193 /* Nothing swapped */
194 return 0;
195 }
196
197 /**
198 * Dump initrd locations (for debug)
199 *
200 */
201 static void initrd_dump ( void ) {
202 struct image *initrd;
203
204 /* Do nothing unless debugging is enabled */
205 if ( ! DBG_LOG )
206 return;
207
208 /* Dump initrd locations */
209 for_each_image ( initrd ) {
210 DBGC ( &images, "INITRD %s at [%#08lx,%#08lx)\n",
211 initrd->name, user_to_phys ( initrd->data, 0 ),
212 user_to_phys ( initrd->data, initrd->len ) );
213 DBGC2_MD5A ( &images, user_to_phys ( initrd->data, 0 ),
214 user_to_virt ( initrd->data, 0 ), initrd->len );
215 }
216 }
217
218 /**
219 * Reshuffle initrds into desired order at top of memory
220 *
221 * @v bottom Lowest address available for initrds
222 *
223 * After this function returns, the initrds have been rearranged in
224 * memory and the external heap structures will have been corrupted.
225 * Reshuffling must therefore take place immediately prior to jumping
226 * to the loaded OS kernel; no further execution within iPXE is
227 * permitted.
228 */
229 void initrd_reshuffle ( userptr_t bottom ) {
230 userptr_t top;
231 userptr_t used;
232 userptr_t free;
233 size_t free_len;
234
235 /* Calculate limits of available space for initrds */
236 top = initrd_top;
237 if ( userptr_sub ( initrd_bottom, bottom ) > 0 )
238 bottom = initrd_bottom;
239
240 /* Debug */
241 DBGC ( &images, "INITRD region [%#08lx,%#08lx)\n",
242 user_to_phys ( bottom, 0 ), user_to_phys ( top, 0 ) );
243 initrd_dump();
244
245 /* Squash initrds as high as possible in memory */
246 used = initrd_squash_high ( top );
247
248 /* Calculate available free space */
249 free = bottom;
250 free_len = userptr_sub ( used, free );
251
252 /* Bubble-sort initrds into desired order */
253 while ( initrd_swap_any ( free, free_len ) ) {}
254
255 /* Debug */
256 initrd_dump();
257 }
258
259 /**
260 * Check that there is enough space to reshuffle initrds
261 *
262 * @v len Total length of initrds (including padding)
263 * @v bottom Lowest address available for initrds
264 * @ret rc Return status code
265 */
266 int initrd_reshuffle_check ( size_t len, userptr_t bottom ) {
267 userptr_t top;
268 size_t available;
269
270 /* Calculate limits of available space for initrds */
271 top = initrd_top;
272 if ( userptr_sub ( initrd_bottom, bottom ) > 0 )
273 bottom = initrd_bottom;
274 available = userptr_sub ( top, bottom );
275
276 /* Allow for a sensible minimum amount of free space */
277 len += INITRD_MIN_FREE_LEN;
278
279 /* Check for available space */
280 return ( ( len < available ) ? 0 : -ENOBUFS );
281 }
282
283 /**
284 * initrd startup function
285 *
286 */
287 static void initrd_startup ( void ) {
288 size_t len;
289
290 /* Record largest memory block available. Do this after any
291 * allocations made during driver startup (e.g. large host
292 * memory blocks for Infiniband devices, which may still be in
293 * use at the time of rearranging if a SAN device is hooked)
294 * but before any allocations for downloaded images (which we
295 * can safely reuse when rearranging).
296 */
297 len = largest_memblock ( &initrd_bottom );
298 initrd_top = userptr_add ( initrd_bottom, len );
299 }
300
301 /** initrd startup function */
302 struct startup_fn startup_initrd __startup_fn ( STARTUP_LATE ) = {
303 .name = "initrd",
304 .startup = initrd_startup,
305 };