e820.c
来自「LINUX 2.6.17.4的源码」· C语言 代码 · 共 682 行 · 第 1/2 页
C
682 行
/* * Handle the memory map. * The functions here do the job until bootmem takes over. * $Id: e820.c,v 1.4 2002/09/19 19:25:32 ak Exp $ * * Getting sanitize_e820_map() in sync with i386 version by applying change: * - Provisions for empty E820 memory regions (reported by certain BIOSes). * Alex Achenbach <xela@slit.de>, December 2002. * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com> * */#include <linux/config.h>#include <linux/kernel.h>#include <linux/types.h>#include <linux/init.h>#include <linux/bootmem.h>#include <linux/ioport.h>#include <linux/string.h>#include <linux/kexec.h>#include <linux/module.h>#include <asm/page.h>#include <asm/e820.h>#include <asm/proto.h>#include <asm/bootsetup.h>#include <asm/sections.h>/* * PFN of last memory page. */unsigned long end_pfn; EXPORT_SYMBOL(end_pfn);/* * end_pfn only includes RAM, while end_pfn_map includes all e820 entries. * The direct mapping extends to end_pfn_map, so that we can directly access * apertures, ACPI and other tables without having to play with fixmaps. */ unsigned long end_pfn_map; /* * Last pfn which the user wants to use. */unsigned long end_user_pfn = MAXMEM>>PAGE_SHIFT; extern struct resource code_resource, data_resource;/* Check for some hardcoded bad areas that early boot is not allowed to touch */ static inline int bad_addr(unsigned long *addrp, unsigned long size){ unsigned long addr = *addrp, last = addr + size; /* various gunk below that needed for SMP startup */ if (addr < 0x8000) { *addrp = 0x8000; return 1; } /* direct mapping tables of the kernel */ if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) { *addrp = table_end << PAGE_SHIFT; return 1; } /* initrd */ #ifdef CONFIG_BLK_DEV_INITRD if (LOADER_TYPE && INITRD_START && last >= INITRD_START && addr < INITRD_START+INITRD_SIZE) { *addrp = INITRD_START + INITRD_SIZE; return 1; } #endif /* kernel code + 640k memory hole (later should not be needed, but be paranoid for now) */ if (last >= 640*1024 && addr < __pa_symbol(&_end)) { *addrp = __pa_symbol(&_end); return 1; } if (last >= ebda_addr && addr < ebda_addr + ebda_size) { *addrp = ebda_addr + ebda_size; return 1; } /* XXX ramdisk image here? */ return 0;} /* * This function checks if any part of the range <start,end> is mapped * with type. */int __meminite820_any_mapped(unsigned long start, unsigned long end, unsigned type){ int i; for (i = 0; i < e820.nr_map; i++) { struct e820entry *ei = &e820.map[i]; if (type && ei->type != type) continue; if (ei->addr >= end || ei->addr + ei->size <= start) continue; return 1; } return 0;}/* * This function checks if the entire range <start,end> is mapped with type. * * Note: this function only works correct if the e820 table is sorted and * not-overlapping, which is the case */int __init e820_all_mapped(unsigned long start, unsigned long end, unsigned type){ int i; for (i = 0; i < e820.nr_map; i++) { struct e820entry *ei = &e820.map[i]; if (type && ei->type != type) continue; /* is the region (part) in overlap with the current region ?*/ if (ei->addr >= end || ei->addr + ei->size <= start) continue; /* if the region is at the beginning of <start,end> we move * start to the end of the region since it's ok until there */ if (ei->addr <= start) start = ei->addr + ei->size; /* if start is now at or beyond end, we're done, full coverage */ if (start >= end) return 1; /* we're done */ } return 0;}/* * Find a free area in a specific range. */ unsigned long __init find_e820_area(unsigned long start, unsigned long end, unsigned size) { int i; for (i = 0; i < e820.nr_map; i++) { struct e820entry *ei = &e820.map[i]; unsigned long addr = ei->addr, last; if (ei->type != E820_RAM) continue; if (addr < start) addr = start; if (addr > ei->addr + ei->size) continue; while (bad_addr(&addr, size) && addr+size <= ei->addr+ei->size) ; last = addr + size; if (last > ei->addr + ei->size) continue; if (last > end) continue; return addr; } return -1UL; } /* * Free bootmem based on the e820 table for a node. */void __init e820_bootmem_free(pg_data_t *pgdat, unsigned long start,unsigned long end){ int i; for (i = 0; i < e820.nr_map; i++) { struct e820entry *ei = &e820.map[i]; unsigned long last, addr; if (ei->type != E820_RAM || ei->addr+ei->size <= start || ei->addr >= end) continue; addr = round_up(ei->addr, PAGE_SIZE); if (addr < start) addr = start; last = round_down(ei->addr + ei->size, PAGE_SIZE); if (last >= end) last = end; if (last > addr && last-addr >= PAGE_SIZE) free_bootmem_node(pgdat, addr, last-addr); }}/* * Find the highest page frame number we have available */unsigned long __init e820_end_of_ram(void){ int i; unsigned long end_pfn = 0; for (i = 0; i < e820.nr_map; i++) { struct e820entry *ei = &e820.map[i]; unsigned long start, end; start = round_up(ei->addr, PAGE_SIZE); end = round_down(ei->addr + ei->size, PAGE_SIZE); if (start >= end) continue; if (ei->type == E820_RAM) { if (end > end_pfn<<PAGE_SHIFT) end_pfn = end>>PAGE_SHIFT; } else { if (end > end_pfn_map<<PAGE_SHIFT) end_pfn_map = end>>PAGE_SHIFT; } } if (end_pfn > end_pfn_map) end_pfn_map = end_pfn; if (end_pfn_map > MAXMEM>>PAGE_SHIFT) end_pfn_map = MAXMEM>>PAGE_SHIFT; if (end_pfn > end_user_pfn) end_pfn = end_user_pfn; if (end_pfn > end_pfn_map) end_pfn = end_pfn_map; return end_pfn; }/* * Compute how much memory is missing in a range. * Unlike the other functions in this file the arguments are in page numbers. */unsigned long __inite820_hole_size(unsigned long start_pfn, unsigned long end_pfn){ unsigned long ram = 0; unsigned long start = start_pfn << PAGE_SHIFT; unsigned long end = end_pfn << PAGE_SHIFT; int i; for (i = 0; i < e820.nr_map; i++) { struct e820entry *ei = &e820.map[i]; unsigned long last, addr; if (ei->type != E820_RAM || ei->addr+ei->size <= start || ei->addr >= end) continue; addr = round_up(ei->addr, PAGE_SIZE); if (addr < start) addr = start; last = round_down(ei->addr + ei->size, PAGE_SIZE); if (last >= end) last = end; if (last > addr) ram += last - addr; } return ((end - start) - ram) >> PAGE_SHIFT;}/* * Mark e820 reserved areas as busy for the resource manager. */void __init e820_reserve_resources(void){ int i; for (i = 0; i < e820.nr_map; i++) { struct resource *res; res = alloc_bootmem_low(sizeof(struct resource)); switch (e820.map[i].type) { case E820_RAM: res->name = "System RAM"; break; case E820_ACPI: res->name = "ACPI Tables"; break; case E820_NVS: res->name = "ACPI Non-volatile Storage"; break; default: res->name = "reserved"; } res->start = e820.map[i].addr; res->end = res->start + e820.map[i].size - 1; res->flags = IORESOURCE_MEM | IORESOURCE_BUSY; request_resource(&iomem_resource, res); if (e820.map[i].type == E820_RAM) { /* * We don't know which RAM region contains kernel data, * so we try it repeatedly and let the resource manager * test it. */ request_resource(res, &code_resource); request_resource(res, &data_resource);#ifdef CONFIG_KEXEC request_resource(res, &crashk_res);#endif } }}/* * Add a memory region to the kernel e820 map. */ void __init add_memory_region(unsigned long start, unsigned long size, int type){ int x = e820.nr_map; if (x == E820MAX) { printk(KERN_ERR "Ooops! Too many entries in the memory map!\n"); return; } e820.map[x].addr = start; e820.map[x].size = size; e820.map[x].type = type; e820.nr_map++;}void __init e820_print_map(char *who){ int i; for (i = 0; i < e820.nr_map; i++) { printk(" %s: %016Lx - %016Lx ", who, (unsigned long long) e820.map[i].addr, (unsigned long long) (e820.map[i].addr + e820.map[i].size)); switch (e820.map[i].type) { case E820_RAM: printk("(usable)\n"); break; case E820_RESERVED: printk("(reserved)\n"); break; case E820_ACPI: printk("(ACPI data)\n"); break; case E820_NVS: printk("(ACPI NVS)\n"); break; default: printk("type %u\n", e820.map[i].type); break; } }}/*
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?