x86: sanitize user specified e820 memmap values
[linux-2.6] / arch / x86 / kernel / e820_64.c
1 /*
2  * Handle the memory map.
3  * The functions here do the job until bootmem takes over.
4  *
5  *  Getting sanitize_e820_map() in sync with i386 version by applying change:
6  *  -  Provisions for empty E820 memory regions (reported by certain BIOSes).
7  *     Alex Achenbach <xela@slit.de>, December 2002.
8  *  Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
9  *
10  */
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/init.h>
14 #include <linux/bootmem.h>
15 #include <linux/ioport.h>
16 #include <linux/string.h>
17 #include <linux/kexec.h>
18 #include <linux/module.h>
19 #include <linux/mm.h>
20 #include <linux/suspend.h>
21 #include <linux/pfn.h>
22
23 #include <asm/pgtable.h>
24 #include <asm/page.h>
25 #include <asm/e820.h>
26 #include <asm/proto.h>
27 #include <asm/setup.h>
28 #include <asm/sections.h>
29 #include <asm/kdebug.h>
30
31 struct e820map e820;
32
33 /*
34  * PFN of last memory page.
35  */
36 unsigned long end_pfn;
37
38 /*
39  * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
40  * The direct mapping extends to end_pfn_map, so that we can directly access
41  * apertures, ACPI and other tables without having to play with fixmaps.
42  */
43 unsigned long end_pfn_map;
44
45 /*
46  * Last pfn which the user wants to use.
47  */
48 static unsigned long __initdata end_user_pfn = MAXMEM>>PAGE_SHIFT;
49
50 /* Check for some hardcoded bad areas that early boot is not allowed to touch */
51 static inline int bad_addr(unsigned long *addrp, unsigned long size)
52 {
53         unsigned long addr = *addrp, last = addr + size;
54
55         /* various gunk below that needed for SMP startup */
56         if (addr < 0x8000) {
57                 *addrp = PAGE_ALIGN(0x8000);
58                 return 1;
59         }
60
61         /* direct mapping tables of the kernel */
62         if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) {
63                 *addrp = PAGE_ALIGN(table_end << PAGE_SHIFT);
64                 return 1;
65         }
66
67         /* initrd */
68 #ifdef CONFIG_BLK_DEV_INITRD
69         if (boot_params.hdr.type_of_loader && boot_params.hdr.ramdisk_image) {
70                 unsigned long ramdisk_image = boot_params.hdr.ramdisk_image;
71                 unsigned long ramdisk_size  = boot_params.hdr.ramdisk_size;
72                 unsigned long ramdisk_end   = ramdisk_image+ramdisk_size;
73
74                 if (last >= ramdisk_image && addr < ramdisk_end) {
75                         *addrp = PAGE_ALIGN(ramdisk_end);
76                         return 1;
77                 }
78         }
79 #endif
80         /* kernel code */
81         if (last >= __pa_symbol(&_text) && addr < __pa_symbol(&_end)) {
82                 *addrp = PAGE_ALIGN(__pa_symbol(&_end));
83                 return 1;
84         }
85
86         if (last >= ebda_addr && addr < ebda_addr + ebda_size) {
87                 *addrp = PAGE_ALIGN(ebda_addr + ebda_size);
88                 return 1;
89         }
90
91 #ifdef CONFIG_NUMA
92         /* NUMA memory to node map */
93         if (last >= nodemap_addr && addr < nodemap_addr + nodemap_size) {
94                 *addrp = nodemap_addr + nodemap_size;
95                 return 1;
96         }
97 #endif
98         /* XXX ramdisk image here? */
99         return 0;
100 }
101
102 /*
103  * This function checks if any part of the range <start,end> is mapped
104  * with type.
105  */
106 int
107 e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
108 {
109         int i;
110
111         for (i = 0; i < e820.nr_map; i++) {
112                 struct e820entry *ei = &e820.map[i];
113
114                 if (type && ei->type != type)
115                         continue;
116                 if (ei->addr >= end || ei->addr + ei->size <= start)
117                         continue;
118                 return 1;
119         }
120         return 0;
121 }
122 EXPORT_SYMBOL_GPL(e820_any_mapped);
123
124 /*
125  * This function checks if the entire range <start,end> is mapped with type.
126  *
127  * Note: this function only works correct if the e820 table is sorted and
128  * not-overlapping, which is the case
129  */
130 int __init e820_all_mapped(unsigned long start, unsigned long end,
131                            unsigned type)
132 {
133         int i;
134
135         for (i = 0; i < e820.nr_map; i++) {
136                 struct e820entry *ei = &e820.map[i];
137
138                 if (type && ei->type != type)
139                         continue;
140                 /* is the region (part) in overlap with the current region ?*/
141                 if (ei->addr >= end || ei->addr + ei->size <= start)
142                         continue;
143
144                 /* if the region is at the beginning of <start,end> we move
145                  * start to the end of the region since it's ok until there
146                  */
147                 if (ei->addr <= start)
148                         start = ei->addr + ei->size;
149                 /*
150                  * if start is now at or beyond end, we're done, full
151                  * coverage
152                  */
153                 if (start >= end)
154                         return 1;
155         }
156         return 0;
157 }
158
159 /*
160  * Find a free area in a specific range.
161  */
162 unsigned long __init find_e820_area(unsigned long start, unsigned long end,
163                                     unsigned size)
164 {
165         int i;
166
167         for (i = 0; i < e820.nr_map; i++) {
168                 struct e820entry *ei = &e820.map[i];
169                 unsigned long addr = ei->addr, last;
170
171                 if (ei->type != E820_RAM)
172                         continue;
173                 if (addr < start)
174                         addr = start;
175                 if (addr > ei->addr + ei->size)
176                         continue;
177                 while (bad_addr(&addr, size) && addr+size <= ei->addr+ei->size)
178                         ;
179                 last = PAGE_ALIGN(addr) + size;
180                 if (last > ei->addr + ei->size)
181                         continue;
182                 if (last > end)
183                         continue;
184                 return addr;
185         }
186         return -1UL;
187 }
188
189 /*
190  * Find the highest page frame number we have available
191  */
192 unsigned long __init e820_end_of_ram(void)
193 {
194         unsigned long end_pfn;
195
196         end_pfn = find_max_pfn_with_active_regions();
197
198         if (end_pfn > end_pfn_map)
199                 end_pfn_map = end_pfn;
200         if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
201                 end_pfn_map = MAXMEM>>PAGE_SHIFT;
202         if (end_pfn > end_user_pfn)
203                 end_pfn = end_user_pfn;
204         if (end_pfn > end_pfn_map)
205                 end_pfn = end_pfn_map;
206
207         printk(KERN_INFO "end_pfn_map = %lu\n", end_pfn_map);
208         return end_pfn;
209 }
210
211 /*
212  * Mark e820 reserved areas as busy for the resource manager.
213  */
214 void __init e820_reserve_resources(struct resource *code_resource,
215                 struct resource *data_resource, struct resource *bss_resource)
216 {
217         int i;
218         for (i = 0; i < e820.nr_map; i++) {
219                 struct resource *res;
220                 res = alloc_bootmem_low(sizeof(struct resource));
221                 switch (e820.map[i].type) {
222                 case E820_RAM:  res->name = "System RAM"; break;
223                 case E820_ACPI: res->name = "ACPI Tables"; break;
224                 case E820_NVS:  res->name = "ACPI Non-volatile Storage"; break;
225                 default:        res->name = "reserved";
226                 }
227                 res->start = e820.map[i].addr;
228                 res->end = res->start + e820.map[i].size - 1;
229                 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
230                 request_resource(&iomem_resource, res);
231                 if (e820.map[i].type == E820_RAM) {
232                         /*
233                          * We don't know which RAM region contains kernel data,
234                          * so we try it repeatedly and let the resource manager
235                          * test it.
236                          */
237                         request_resource(res, code_resource);
238                         request_resource(res, data_resource);
239                         request_resource(res, bss_resource);
240 #ifdef CONFIG_KEXEC
241                         if (crashk_res.start != crashk_res.end)
242                                 request_resource(res, &crashk_res);
243 #endif
244                 }
245         }
246 }
247
248 /*
249  * Find the ranges of physical addresses that do not correspond to
250  * e820 RAM areas and mark the corresponding pages as nosave for software
251  * suspend and suspend to RAM.
252  *
253  * This function requires the e820 map to be sorted and without any
254  * overlapping entries and assumes the first e820 area to be RAM.
255  */
256 void __init e820_mark_nosave_regions(void)
257 {
258         int i;
259         unsigned long paddr;
260
261         paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
262         for (i = 1; i < e820.nr_map; i++) {
263                 struct e820entry *ei = &e820.map[i];
264
265                 if (paddr < ei->addr)
266                         register_nosave_region(PFN_DOWN(paddr),
267                                                 PFN_UP(ei->addr));
268
269                 paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
270                 if (ei->type != E820_RAM)
271                         register_nosave_region(PFN_UP(ei->addr),
272                                                 PFN_DOWN(paddr));
273
274                 if (paddr >= (end_pfn << PAGE_SHIFT))
275                         break;
276         }
277 }
278
279 /*
280  * Finds an active region in the address range from start_pfn to end_pfn and
281  * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
282  */
283 static int __init e820_find_active_region(const struct e820entry *ei,
284                                           unsigned long start_pfn,
285                                           unsigned long end_pfn,
286                                           unsigned long *ei_startpfn,
287                                           unsigned long *ei_endpfn)
288 {
289         *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
290         *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
291
292         /* Skip map entries smaller than a page */
293         if (*ei_startpfn >= *ei_endpfn)
294                 return 0;
295
296         /* Check if end_pfn_map should be updated */
297         if (ei->type != E820_RAM && *ei_endpfn > end_pfn_map)
298                 end_pfn_map = *ei_endpfn;
299
300         /* Skip if map is outside the node */
301         if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
302                                     *ei_startpfn >= end_pfn)
303                 return 0;
304
305         /* Check for overlaps */
306         if (*ei_startpfn < start_pfn)
307                 *ei_startpfn = start_pfn;
308         if (*ei_endpfn > end_pfn)
309                 *ei_endpfn = end_pfn;
310
311         /* Obey end_user_pfn to save on memmap */
312         if (*ei_startpfn >= end_user_pfn)
313                 return 0;
314         if (*ei_endpfn > end_user_pfn)
315                 *ei_endpfn = end_user_pfn;
316
317         return 1;
318 }
319
320 /* Walk the e820 map and register active regions within a node */
321 void __init
322 e820_register_active_regions(int nid, unsigned long start_pfn,
323                                                         unsigned long end_pfn)
324 {
325         unsigned long ei_startpfn;
326         unsigned long ei_endpfn;
327         int i;
328
329         for (i = 0; i < e820.nr_map; i++)
330                 if (e820_find_active_region(&e820.map[i],
331                                             start_pfn, end_pfn,
332                                             &ei_startpfn, &ei_endpfn))
333                         add_active_range(nid, ei_startpfn, ei_endpfn);
334 }
335
336 /*
337  * Add a memory region to the kernel e820 map.
338  */
339 void __init add_memory_region(unsigned long start, unsigned long size, int type)
340 {
341         int x = e820.nr_map;
342
343         if (x == E820MAX) {
344                 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
345                 return;
346         }
347
348         e820.map[x].addr = start;
349         e820.map[x].size = size;
350         e820.map[x].type = type;
351         e820.nr_map++;
352 }
353
354 /*
355  * Find the hole size (in bytes) in the memory range.
356  * @start: starting address of the memory range to scan
357  * @end: ending address of the memory range to scan
358  */
359 unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
360 {
361         unsigned long start_pfn = start >> PAGE_SHIFT;
362         unsigned long end_pfn = end >> PAGE_SHIFT;
363         unsigned long ei_startpfn, ei_endpfn, ram = 0;
364         int i;
365
366         for (i = 0; i < e820.nr_map; i++) {
367                 if (e820_find_active_region(&e820.map[i],
368                                             start_pfn, end_pfn,
369                                             &ei_startpfn, &ei_endpfn))
370                         ram += ei_endpfn - ei_startpfn;
371         }
372         return end - start - (ram << PAGE_SHIFT);
373 }
374
375 static void __init e820_print_map(char *who)
376 {
377         int i;
378
379         for (i = 0; i < e820.nr_map; i++) {
380                 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
381                        (unsigned long long) e820.map[i].addr,
382                        (unsigned long long)
383                        (e820.map[i].addr + e820.map[i].size));
384                 switch (e820.map[i].type) {
385                 case E820_RAM:
386                         printk(KERN_CONT "(usable)\n");
387                         break;
388                 case E820_RESERVED:
389                         printk(KERN_CONT "(reserved)\n");
390                         break;
391                 case E820_ACPI:
392                         printk(KERN_CONT "(ACPI data)\n");
393                         break;
394                 case E820_NVS:
395                         printk(KERN_CONT "(ACPI NVS)\n");
396                         break;
397                 default:
398                         printk(KERN_CONT "type %u\n", e820.map[i].type);
399                         break;
400                 }
401         }
402 }
403
404 /*
405  * Sanitize the BIOS e820 map.
406  *
407  * Some e820 responses include overlapping entries. The following
408  * replaces the original e820 map with a new one, removing overlaps.
409  *
410  */
411 static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
412 {
413         struct change_member {
414                 struct e820entry *pbios; /* pointer to original bios entry */
415                 unsigned long long addr; /* address for this change point */
416         };
417         static struct change_member change_point_list[2*E820MAX] __initdata;
418         static struct change_member *change_point[2*E820MAX] __initdata;
419         static struct e820entry *overlap_list[E820MAX] __initdata;
420         static struct e820entry new_bios[E820MAX] __initdata;
421         struct change_member *change_tmp;
422         unsigned long current_type, last_type;
423         unsigned long long last_addr;
424         int chgidx, still_changing;
425         int overlap_entries;
426         int new_bios_entry;
427         int old_nr, new_nr, chg_nr;
428         int i;
429
430         /*
431                 Visually we're performing the following
432                 (1,2,3,4 = memory types)...
433
434                 Sample memory map (w/overlaps):
435                    ____22__________________
436                    ______________________4_
437                    ____1111________________
438                    _44_____________________
439                    11111111________________
440                    ____________________33__
441                    ___________44___________
442                    __________33333_________
443                    ______________22________
444                    ___________________2222_
445                    _________111111111______
446                    _____________________11_
447                    _________________4______
448
449                 Sanitized equivalent (no overlap):
450                    1_______________________
451                    _44_____________________
452                    ___1____________________
453                    ____22__________________
454                    ______11________________
455                    _________1______________
456                    __________3_____________
457                    ___________44___________
458                    _____________33_________
459                    _______________2________
460                    ________________1_______
461                    _________________4______
462                    ___________________2____
463                    ____________________33__
464                    ______________________4_
465         */
466
467         /* if there's only one memory region, don't bother */
468         if (*pnr_map < 2)
469                 return -1;
470
471         old_nr = *pnr_map;
472
473         /* bail out if we find any unreasonable addresses in bios map */
474         for (i = 0; i < old_nr; i++)
475                 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
476                         return -1;
477
478         /* create pointers for initial change-point information (for sorting) */
479         for (i = 0; i < 2 * old_nr; i++)
480                 change_point[i] = &change_point_list[i];
481
482         /* record all known change-points (starting and ending addresses),
483            omitting those that are for empty memory regions */
484         chgidx = 0;
485         for (i = 0; i < old_nr; i++)    {
486                 if (biosmap[i].size != 0) {
487                         change_point[chgidx]->addr = biosmap[i].addr;
488                         change_point[chgidx++]->pbios = &biosmap[i];
489                         change_point[chgidx]->addr = biosmap[i].addr +
490                                 biosmap[i].size;
491                         change_point[chgidx++]->pbios = &biosmap[i];
492                 }
493         }
494         chg_nr = chgidx;
495
496         /* sort change-point list by memory addresses (low -> high) */
497         still_changing = 1;
498         while (still_changing)  {
499                 still_changing = 0;
500                 for (i = 1; i < chg_nr; i++)  {
501                         unsigned long long curaddr, lastaddr;
502                         unsigned long long curpbaddr, lastpbaddr;
503
504                         curaddr = change_point[i]->addr;
505                         lastaddr = change_point[i - 1]->addr;
506                         curpbaddr = change_point[i]->pbios->addr;
507                         lastpbaddr = change_point[i - 1]->pbios->addr;
508
509                         /*
510                          * swap entries, when:
511                          *
512                          * curaddr > lastaddr or
513                          * curaddr == lastaddr and curaddr == curpbaddr and
514                          * lastaddr != lastpbaddr
515                          */
516                         if (curaddr < lastaddr ||
517                             (curaddr == lastaddr && curaddr == curpbaddr &&
518                              lastaddr != lastpbaddr)) {
519                                 change_tmp = change_point[i];
520                                 change_point[i] = change_point[i-1];
521                                 change_point[i-1] = change_tmp;
522                                 still_changing = 1;
523                         }
524                 }
525         }
526
527         /* create a new bios memory map, removing overlaps */
528         overlap_entries = 0;     /* number of entries in the overlap table */
529         new_bios_entry = 0;      /* index for creating new bios map entries */
530         last_type = 0;           /* start with undefined memory type */
531         last_addr = 0;           /* start with 0 as last starting address */
532
533         /* loop through change-points, determining affect on the new bios map */
534         for (chgidx = 0; chgidx < chg_nr; chgidx++) {
535                 /* keep track of all overlapping bios entries */
536                 if (change_point[chgidx]->addr ==
537                     change_point[chgidx]->pbios->addr) {
538                         /*
539                          * add map entry to overlap list (> 1 entry
540                          * implies an overlap)
541                          */
542                         overlap_list[overlap_entries++] =
543                                 change_point[chgidx]->pbios;
544                 } else {
545                         /*
546                          * remove entry from list (order independent,
547                          * so swap with last)
548                          */
549                         for (i = 0; i < overlap_entries; i++) {
550                                 if (overlap_list[i] ==
551                                     change_point[chgidx]->pbios)
552                                         overlap_list[i] =
553                                                 overlap_list[overlap_entries-1];
554                         }
555                         overlap_entries--;
556                 }
557                 /*
558                  * if there are overlapping entries, decide which
559                  * "type" to use (larger value takes precedence --
560                  * 1=usable, 2,3,4,4+=unusable)
561                  */
562                 current_type = 0;
563                 for (i = 0; i < overlap_entries; i++)
564                         if (overlap_list[i]->type > current_type)
565                                 current_type = overlap_list[i]->type;
566                 /*
567                  * continue building up new bios map based on this
568                  * information
569                  */
570                 if (current_type != last_type)  {
571                         if (last_type != 0)      {
572                                 new_bios[new_bios_entry].size =
573                                         change_point[chgidx]->addr - last_addr;
574                                 /*
575                                  * move forward only if the new size
576                                  * was non-zero
577                                  */
578                                 if (new_bios[new_bios_entry].size != 0)
579                                         /*
580                                          * no more space left for new
581                                          * bios entries ?
582                                          */
583                                         if (++new_bios_entry >= E820MAX)
584                                                 break;
585                         }
586                         if (current_type != 0)  {
587                                 new_bios[new_bios_entry].addr =
588                                         change_point[chgidx]->addr;
589                                 new_bios[new_bios_entry].type = current_type;
590                                 last_addr = change_point[chgidx]->addr;
591                         }
592                         last_type = current_type;
593                 }
594         }
595         /* retain count for new bios entries */
596         new_nr = new_bios_entry;
597
598         /* copy new bios mapping into original location */
599         memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
600         *pnr_map = new_nr;
601
602         return 0;
603 }
604
605 /*
606  * Copy the BIOS e820 map into a safe place.
607  *
608  * Sanity-check it while we're at it..
609  *
610  * If we're lucky and live on a modern system, the setup code
611  * will have given us a memory map that we can use to properly
612  * set up memory.  If we aren't, we'll fake a memory map.
613  */
614 static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
615 {
616         /* Only one memory region (or negative)? Ignore it */
617         if (nr_map < 2)
618                 return -1;
619
620         do {
621                 unsigned long start = biosmap->addr;
622                 unsigned long size = biosmap->size;
623                 unsigned long end = start + size;
624                 unsigned long type = biosmap->type;
625
626                 /* Overflow in 64 bits? Ignore the memory map. */
627                 if (start > end)
628                         return -1;
629
630                 add_memory_region(start, size, type);
631         } while (biosmap++, --nr_map);
632         return 0;
633 }
634
635 static void early_panic(char *msg)
636 {
637         early_printk(msg);
638         panic(msg);
639 }
640
641 void __init setup_memory_region(void)
642 {
643         /*
644          * Try to copy the BIOS-supplied E820-map.
645          *
646          * Otherwise fake a memory map; one section from 0k->640k,
647          * the next section from 1mb->appropriate_mem_k
648          */
649         sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
650         if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
651                 early_panic("Cannot find a valid memory map");
652         printk(KERN_INFO "BIOS-provided physical RAM map:\n");
653         e820_print_map("BIOS-e820");
654 }
655
656 static int __init parse_memopt(char *p)
657 {
658         if (!p)
659                 return -EINVAL;
660         end_user_pfn = memparse(p, &p);
661         end_user_pfn >>= PAGE_SHIFT;
662         return 0;
663 }
664 early_param("mem", parse_memopt);
665
666 static int userdef __initdata;
667
668 static int __init parse_memmap_opt(char *p)
669 {
670         char *oldp;
671         unsigned long long start_at, mem_size;
672
673         if (!strcmp(p, "exactmap")) {
674 #ifdef CONFIG_CRASH_DUMP
675                 /*
676                  * If we are doing a crash dump, we still need to know
677                  * the real mem size before original memory map is
678                  * reset.
679                  */
680                 e820_register_active_regions(0, 0, -1UL);
681                 saved_max_pfn = e820_end_of_ram();
682                 remove_all_active_ranges();
683 #endif
684                 end_pfn_map = 0;
685                 e820.nr_map = 0;
686                 userdef = 1;
687                 return 0;
688         }
689
690         oldp = p;
691         mem_size = memparse(p, &p);
692         if (p == oldp)
693                 return -EINVAL;
694
695         userdef = 1;
696         if (*p == '@') {
697                 start_at = memparse(p+1, &p);
698                 add_memory_region(start_at, mem_size, E820_RAM);
699         } else if (*p == '#') {
700                 start_at = memparse(p+1, &p);
701                 add_memory_region(start_at, mem_size, E820_ACPI);
702         } else if (*p == '$') {
703                 start_at = memparse(p+1, &p);
704                 add_memory_region(start_at, mem_size, E820_RESERVED);
705         } else {
706                 end_user_pfn = (mem_size >> PAGE_SHIFT);
707         }
708         return *p == '\0' ? 0 : -EINVAL;
709 }
710 early_param("memmap", parse_memmap_opt);
711
712 void __init finish_e820_parsing(void)
713 {
714         if (userdef) {
715                 char nr = e820.nr_map;
716
717                 if (sanitize_e820_map(e820.map, &nr) < 0)
718                         early_panic("Invalid user supplied memory map");
719                 e820.nr_map = nr;
720
721                 printk(KERN_INFO "user-defined physical RAM map:\n");
722                 e820_print_map("user");
723         }
724 }
725
726 unsigned long pci_mem_start = 0xaeedbabe;
727 EXPORT_SYMBOL(pci_mem_start);
728
729 /*
730  * Search for the biggest gap in the low 32 bits of the e820
731  * memory space.  We pass this space to PCI to assign MMIO resources
732  * for hotplug or unconfigured devices in.
733  * Hopefully the BIOS let enough space left.
734  */
735 __init void e820_setup_gap(void)
736 {
737         unsigned long gapstart, gapsize, round;
738         unsigned long last;
739         int i;
740         int found = 0;
741
742         last = 0x100000000ull;
743         gapstart = 0x10000000;
744         gapsize = 0x400000;
745         i = e820.nr_map;
746         while (--i >= 0) {
747                 unsigned long long start = e820.map[i].addr;
748                 unsigned long long end = start + e820.map[i].size;
749
750                 /*
751                  * Since "last" is at most 4GB, we know we'll
752                  * fit in 32 bits if this condition is true
753                  */
754                 if (last > end) {
755                         unsigned long gap = last - end;
756
757                         if (gap > gapsize) {
758                                 gapsize = gap;
759                                 gapstart = end;
760                                 found = 1;
761                         }
762                 }
763                 if (start < last)
764                         last = start;
765         }
766
767         if (!found) {
768                 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
769                 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
770                        "address range\n"
771                        KERN_ERR "PCI: Unassigned devices with 32bit resource "
772                        "registers may break!\n");
773         }
774
775         /*
776          * See how much we want to round up: start off with
777          * rounding to the next 1MB area.
778          */
779         round = 0x100000;
780         while ((gapsize >> 4) > round)
781                 round += round;
782         /* Fun with two's complement */
783         pci_mem_start = (gapstart + round) & -round;
784
785         printk(KERN_INFO
786                "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
787                pci_mem_start, gapstart, gapsize);
788 }
789
790 int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
791 {
792         int i;
793
794         if (slot < 0 || slot >= e820.nr_map)
795                 return -1;
796         for (i = slot; i < e820.nr_map; i++) {
797                 if (e820.map[i].type != E820_RAM)
798                         continue;
799                 break;
800         }
801         if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
802                 return -1;
803         *addr = e820.map[i].addr;
804         *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
805                 max_pfn << PAGE_SHIFT) - *addr;
806         return i + 1;
807 }