summaryrefslogtreecommitdiffstats
path: root/meta/recipes-devtools/syslinux/files/0002-memscan-build-a-linked-list-of-memory-scanners.patch
blob: 48d3955910882ffeac85a0eaf4e495e8dacd3b53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
Upstream-Status: Backport [787d7e568fe08d7080d2cd03cd9ee27c327eca67]
Signed-off-by: Jonathan Liu <net147@gmail.com>

From 2e05f34c0c5bc0144bb203a169009dfb6837b4e3 Mon Sep 17 00:00:00 2001
From: Matt Fleming <matt.fleming@intel.com>
Date: Wed, 17 Jul 2013 12:15:16 +0100
Subject: [PATCH 2/4] memscan: build a linked list of memory scanners

By registering memory scanners at runtime we can support multiple memory
scanner functions, which helps us to isolate them and keep things
modular, only registering them for specific platform/derivative
combinations. This is preparation for adding a memory scanner that is
specific to PXELINUX on bios and understands when the memory region
occupied by the PXE stack can be reused.

Signed-off-by: Matt Fleming <matt.fleming@intel.com>

Conflicts:
	com32/include/syslinux/memscan.h
	com32/lib/syslinux/memscan.c
---
 com32/include/syslinux/firmware.h |   4 +-
 com32/include/syslinux/memscan.h  |  14 +++-
 com32/lib/syslinux/memscan.c      | 143 +++++++-------------------------------
 core/bios.c                       | 125 ++++++++++++++++++++++++++++++++-
 efi/main.c                        |  14 ++--
 5 files changed, 173 insertions(+), 127 deletions(-)

diff --git a/com32/include/syslinux/firmware.h b/com32/include/syslinux/firmware.h
index 4a43c86..6cc06a0 100644
--- a/com32/include/syslinux/firmware.h
+++ b/com32/include/syslinux/firmware.h
@@ -1,7 +1,8 @@
 #ifndef _SYSLINUX_FIRMWARE_H
 #define _SYSLINUX_FIRMWARE_H
 
-#include <syslinux/memscan.h>
+#include <inttypes.h>
+#include <stdbool.h>
 
 struct term_state;
 
@@ -42,7 +43,6 @@ struct mem_ops {
 	void *(*malloc)(size_t, enum heap, size_t);
 	void *(*realloc)(void *, size_t);
 	void (*free)(void *);
-	int (*scan_memory)(scan_memory_callback_t, void *);
 };
 
 struct initramfs;
diff --git a/com32/include/syslinux/memscan.h b/com32/include/syslinux/memscan.h
index c3ebf84..ab78e28 100644
--- a/com32/include/syslinux/memscan.h
+++ b/com32/include/syslinux/memscan.h
@@ -29,11 +29,19 @@
 #ifndef _SYSLINUX_MEMSCAN_H
 #define _SYSLINUX_MEMSCAN_H
 
-#include <stdbool.h>
+#include <linux/list.h>
 #include <syslinux/movebits.h>	/* addr_t */
 
-typedef int (*scan_memory_callback_t) (void *, addr_t, addr_t, bool);
+typedef int (*scan_memory_callback_t) (void *, addr_t, addr_t,
+				       enum syslinux_memmap_types type);
+
+struct syslinux_memscan {
+    int (*func)(scan_memory_callback_t callback, void *data);
+    struct list_head next;
+};
+
+void syslinux_memscan_add(struct syslinux_memscan *entry);
+int syslinux_memscan_new(int (*func)(scan_memory_callback_t cb, void *data));
 int syslinux_scan_memory(scan_memory_callback_t callback, void *data);
-int bios_scan_memory(scan_memory_callback_t callback, void *data);
 
 #endif /* _SYSLINUX_MEMSCAN_H */
diff --git a/com32/lib/syslinux/memscan.c b/com32/lib/syslinux/memscan.c
index 0ff25d7..fdb7274 100644
--- a/com32/lib/syslinux/memscan.c
+++ b/com32/lib/syslinux/memscan.c
@@ -32,133 +32,44 @@
  * Query the system for free memory
  */
 
-#include <assert.h>
-#include <stdbool.h>
-#include <stdlib.h>
-#include <string.h>
-#include <inttypes.h>
-#include <com32.h>
-
 #include <syslinux/memscan.h>
-#include <syslinux/firmware.h>
 
-struct e820_entry {
-    uint64_t start;
-    uint64_t len;
-    uint32_t type;
-};
+static LIST_HEAD(syslinux_memscan_head);
 
-int bios_scan_memory(scan_memory_callback_t callback, void *data)
+/*
+ * Add a memscan entry to the list.
+ */
+void syslinux_memscan_add(struct syslinux_memscan *entry)
 {
-    static com32sys_t ireg;
-    com32sys_t oreg;
-    struct e820_entry *e820buf;
-    uint64_t start, len, maxlen;
-    int memfound = 0;
-    int rv;
-    addr_t dosmem;
-    const addr_t bios_data = 0x510;	/* Amount to reserve for BIOS data */
+    list_add(&entry->next, &syslinux_memscan_head);
+}
 
-    /* Use INT 12h to get DOS memory */
-    __intcall(0x12, &__com32_zero_regs, &oreg);
-    dosmem = oreg.eax.w[0] << 10;
-    if (dosmem < 32 * 1024 || dosmem > 640 * 1024) {
-	/* INT 12h reports nonsense... now what? */
-	uint16_t ebda_seg = *(uint16_t *) 0x40e;
-	if (ebda_seg >= 0x8000 && ebda_seg < 0xa000)
-	    dosmem = ebda_seg << 4;
-	else
-	    dosmem = 640 * 1024;	/* Hope for the best... */
-    }
-    rv = callback(data, bios_data, dosmem - bios_data, true);
-    if (rv)
-	return rv;
+/*
+ * Build a new memscan entry and add it to the list.
+ */
+int syslinux_memscan_new(int func(scan_memory_callback_t, void *data))
+{
+    struct syslinux_memscan *entry;
 
-    /* First try INT 15h AX=E820h */
-    e820buf = lzalloc(sizeof *e820buf);
-    if (!e820buf)
+    entry = malloc(sizeof *entry);
+    if (!entry)
 	return -1;
 
-    ireg.eax.l = 0xe820;
-    ireg.edx.l = 0x534d4150;
-    ireg.ebx.l = 0;
-    ireg.ecx.l = sizeof(*e820buf);
-    ireg.es = SEG(e820buf);
-    ireg.edi.w[0] = OFFS(e820buf);
-
-    do {
-	__intcall(0x15, &ireg, &oreg);
-
-	if ((oreg.eflags.l & EFLAGS_CF) ||
-	    (oreg.eax.l != 0x534d4150) || (oreg.ecx.l < 20))
-	    break;
-
-	start = e820buf->start;
-	len = e820buf->len;
-
-	if (start < 0x100000000ULL) {
-	    /* Don't rely on E820 being valid for low memory.  Doing so
-	       could mean stuff like overwriting the PXE stack even when
-	       using "keeppxe", etc. */
-	    if (start < 0x100000ULL) {
-		if (len > 0x100000ULL - start)
-		    len -= 0x100000ULL - start;
-		else
-		    len = 0;
-		start = 0x100000ULL;
-	    }
-
-	    maxlen = 0x100000000ULL - start;
-	    if (len > maxlen)
-		len = maxlen;
-
-	    if (len) {
-		rv = callback(data, (addr_t) start, (addr_t) len,
-			      e820buf->type == 1);
-		if (rv)
-		    return rv;
-		memfound = 1;
-	    }
-	}
-
-	ireg.ebx.l = oreg.ebx.l;
-    } while (oreg.ebx.l);
-
-    lfree(e820buf);
-
-    if (memfound)
-	return 0;
-
-    /* Next try INT 15h AX=E801h */
-    ireg.eax.w[0] = 0xe801;
-    __intcall(0x15, &ireg, &oreg);
-
-    if (!(oreg.eflags.l & EFLAGS_CF) && oreg.ecx.w[0]) {
-	rv = callback(data, (addr_t) 1 << 20, oreg.ecx.w[0] << 10, true);
-	if (rv)
-	    return rv;
-
-	if (oreg.edx.w[0]) {
-	    rv = callback(data, (addr_t) 16 << 20, oreg.edx.w[0] << 16, true);
-	    if (rv)
-		return rv;
-	}
-
-	return 0;
-    }
-
-    /* Finally try INT 15h AH=88h */
-    ireg.eax.w[0] = 0x8800;
-    if (!(oreg.eflags.l & EFLAGS_CF) && oreg.eax.w[0]) {
-	rv = callback(data, (addr_t) 1 << 20, oreg.ecx.w[0] << 10, true);
-	if (rv)
-	    return rv;
-    }
-
+    entry->func = func;
+    syslinux_memscan_add(entry);
     return 0;
 }
 
 int syslinux_scan_memory(scan_memory_callback_t callback, void *data)
 {
-	return firmware->mem->scan_memory(callback, data);
+    struct syslinux_memscan *entry;
+    int rv = 0;
+
+    list_for_each_entry(entry, &syslinux_memscan_head, next) {
+	rv = entry->func(callback, data);
+	if (rv)
+	    break;
+    }
+
+    return rv;
 }
diff --git a/core/bios.c b/core/bios.c
index 53223e8..5344833 100644
--- a/core/bios.c
+++ b/core/bios.c
@@ -501,6 +501,129 @@ static inline void bios_timer_init(void)
 
 extern uint16_t *bios_free_mem;
 
+struct e820_entry {
+    uint64_t start;
+    uint64_t len;
+    uint32_t type;
+};
+
+static int bios_scan_memory(scan_memory_callback_t callback, void *data)
+{
+    static com32sys_t ireg;
+    com32sys_t oreg;
+    struct e820_entry *e820buf;
+    uint64_t start, len, maxlen;
+    int memfound = 0;
+    int rv;
+    addr_t dosmem;
+    const addr_t bios_data = 0x510;	/* Amount to reserve for BIOS data */
+
+    /* Use INT 12h to get DOS memory */
+    __intcall(0x12, &__com32_zero_regs, &oreg);
+    dosmem = oreg.eax.w[0] << 10;
+    if (dosmem < 32 * 1024 || dosmem > 640 * 1024) {
+	/* INT 12h reports nonsense... now what? */
+	uint16_t ebda_seg = *(uint16_t *) 0x40e;
+	if (ebda_seg >= 0x8000 && ebda_seg < 0xa000)
+	    dosmem = ebda_seg << 4;
+	else
+	    dosmem = 640 * 1024;	/* Hope for the best... */
+    }
+    rv = callback(data, bios_data, dosmem - bios_data, SMT_FREE);
+    if (rv)
+	return rv;
+
+    /* First try INT 15h AX=E820h */
+    e820buf = lzalloc(sizeof *e820buf);
+    if (!e820buf)
+	return -1;
+
+    ireg.eax.l = 0xe820;
+    ireg.edx.l = 0x534d4150;
+    ireg.ebx.l = 0;
+    ireg.ecx.l = sizeof(*e820buf);
+    ireg.es = SEG(e820buf);
+    ireg.edi.w[0] = OFFS(e820buf);
+
+    do {
+	__intcall(0x15, &ireg, &oreg);
+
+	if ((oreg.eflags.l & EFLAGS_CF) ||
+	    (oreg.eax.l != 0x534d4150) || (oreg.ecx.l < 20))
+	    break;
+
+	start = e820buf->start;
+	len = e820buf->len;
+
+	if (start < 0x100000000ULL) {
+	    /* Don't rely on E820 being valid for low memory.  Doing so
+	       could mean stuff like overwriting the PXE stack even when
+	       using "keeppxe", etc. */
+	    if (start < 0x100000ULL) {
+		if (len > 0x100000ULL - start)
+		    len -= 0x100000ULL - start;
+		else
+		    len = 0;
+		start = 0x100000ULL;
+	    }
+
+	    maxlen = 0x100000000ULL - start;
+	    if (len > maxlen)
+		len = maxlen;
+
+	    if (len) {
+		enum syslinux_memmap_types type;
+
+		type = e820buf->type == 1 ? SMT_FREE : SMT_RESERVED;
+		rv = callback(data, (addr_t) start, (addr_t) len, type);
+		if (rv)
+		    return rv;
+		memfound = 1;
+	    }
+	}
+
+	ireg.ebx.l = oreg.ebx.l;
+    } while (oreg.ebx.l);
+
+    lfree(e820buf);
+
+    if (memfound)
+	return 0;
+
+    /* Next try INT 15h AX=E801h */
+    ireg.eax.w[0] = 0xe801;
+    __intcall(0x15, &ireg, &oreg);
+
+    if (!(oreg.eflags.l & EFLAGS_CF) && oreg.ecx.w[0]) {
+	rv = callback(data, (addr_t) 1 << 20, oreg.ecx.w[0] << 10, SMT_FREE);
+	if (rv)
+	    return rv;
+
+	if (oreg.edx.w[0]) {
+	    rv = callback(data, (addr_t) 16 << 20,
+			  oreg.edx.w[0] << 16, SMT_FREE);
+	    if (rv)
+		return rv;
+	}
+
+	return 0;
+    }
+
+    /* Finally try INT 15h AH=88h */
+    ireg.eax.w[0] = 0x8800;
+    if (!(oreg.eflags.l & EFLAGS_CF) && oreg.eax.w[0]) {
+	rv = callback(data, (addr_t) 1 << 20, oreg.ecx.w[0] << 10, SMT_FREE);
+	if (rv)
+	    return rv;
+    }
+
+    return 0;
+}
+
+static struct syslinux_memscan bios_memscan = {
+    .func = bios_scan_memory,
+};
+
 void bios_init(void)
 {
 	int i;
@@ -515,6 +638,7 @@ void bios_init(void)
 
 	/* Init the memory subsystem */
 	bios_free_mem = (uint16_t *)0x413;
+	syslinux_memscan_add(&bios_memscan);
 	mem_init();
 
 	/* CPU-dependent initialization and related checks. */
@@ -534,7 +658,6 @@ struct mem_ops bios_mem_ops = {
 	.malloc = bios_malloc,
 	.realloc = bios_realloc,
 	.free = bios_free,
-	.scan_memory = bios_scan_memory,
 };
 
 struct firmware bios_fw = {
diff --git a/efi/main.c b/efi/main.c
index 13b9403..fb8cf05 100644
--- a/efi/main.c
+++ b/efi/main.c
@@ -296,21 +296,21 @@ int efi_scan_memory(scan_memory_callback_t callback, void *data)
 	for (i = 0; i < nr_entries; bufpos += desc_sz, i++) {
 		EFI_MEMORY_DESCRIPTOR *m;
 		UINT64 region_sz;
-		int valid;
+		enum syslinux_memmap_types type;
 
 		m = (EFI_MEMORY_DESCRIPTOR *)bufpos;
 		region_sz = m->NumberOfPages * EFI_PAGE_SIZE;
 
 		switch (m->Type) {
                 case EfiConventionalMemory:
-			valid = 1;
+			type = SMT_FREE;
                         break;
 		default:
-			valid = 0;
+			type = SMT_RESERVED;
 			break;
 		}
 
-		rv = callback(data, m->PhysicalStart, region_sz, valid);
+		rv = callback(data, m->PhysicalStart, region_sz, type);
 		if (rv)
 			break;
 	}
@@ -319,11 +319,16 @@ int efi_scan_memory(scan_memory_callback_t callback, void *data)
 	return rv;
 }
 
+static struct syslinux_memscan efi_memscan = {
+    .func = efi_scan_memory,
+};
+
 extern uint16_t *bios_free_mem;
 void efi_init(void)
 {
 	/* XXX timer */
 	*bios_free_mem = 0;
+	syslinux_memscan_add(&efi_memscan);
 	mem_init();
 }
 
@@ -1103,7 +1108,6 @@ struct mem_ops efi_mem_ops = {
 	.malloc = efi_malloc,
 	.realloc = efi_realloc,
 	.free = efi_free,
-	.scan_memory = efi_scan_memory,
 };
 
 struct firmware efi_fw = {
-- 
1.8.5.3

OpenPOWER on IntegriCloud