/* Memory map would allow PCI passthrough. */ bool xen_pv_pci_possible;
/* E820 map used during setting up memory. */ staticstruct e820_table xen_e820_table __initdata;
/* Number of initially usable memory pages. */ staticunsignedlong ini_nr_pages __initdata;
/* * Buffer used to remap identity mapped pages. We only need the virtual space. * The physical page behind this address is remapped as needed to different * buffer pages.
*/ #define REMAP_SIZE (P2M_PER_PAGE - 3) staticstruct { unsignedlong next_area_mfn; unsignedlong target_pfn; unsignedlong size; unsignedlong mfns[REMAP_SIZE];
} xen_remap_buf __initdata __aligned(PAGE_SIZE); staticunsignedlong xen_remap_mfn __initdata = INVALID_P2M_ENTRY;
/* * Called during boot before the p2m list can take entries beyond the * hypervisor supplied p2m list. Entries in extra mem are to be regarded as * invalid.
*/ unsignedlong __ref xen_chk_extra_mem(unsignedlong pfn)
{ int i;
for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) { if (pfn >= xen_extra_mem[i].start_pfn &&
pfn < xen_extra_mem[i].start_pfn + xen_extra_mem[i].n_pfns) return INVALID_P2M_ENTRY;
}
return IDENTITY_FRAME(pfn);
}
/* * Mark all pfns of extra mem as invalid in p2m list.
*/ void __init xen_inv_extra_mem(void)
{ unsignedlong pfn, pfn_s, pfn_e; int i;
for (i = 0; i < XEN_EXTRA_MEM_MAX_REGIONS; i++) { if (!xen_extra_mem[i].n_pfns) continue;
pfn_s = xen_extra_mem[i].start_pfn;
pfn_e = pfn_s + xen_extra_mem[i].n_pfns; for (pfn = pfn_s; pfn < pfn_e; pfn++)
set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
}
}
/* * Finds the next RAM pfn available in the E820 map after min_pfn. * This function updates min_pfn with the pfn found and returns * the size of that range or zero if not found.
*/ staticunsignedlong __init xen_find_pfn_range(unsignedlong *min_pfn)
{ conststruct e820_entry *entry = xen_e820_table.entries; unsignedint i; unsignedlong done = 0;
for (i = 0; i < xen_e820_table.nr_entries; i++, entry++) { unsignedlong s_pfn; unsignedlong e_pfn;
if (entry->type != E820_TYPE_RAM) continue;
e_pfn = PFN_DOWN(entry->addr + entry->size);
/* We only care about E820 after this */ if (e_pfn <= *min_pfn) continue;
s_pfn = PFN_UP(entry->addr);
/* If min_pfn falls within the E820 entry, we want to start * at the min_pfn PFN.
*/ if (s_pfn <= *min_pfn) {
done = e_pfn - *min_pfn;
} else {
done = e_pfn - s_pfn;
*min_pfn = s_pfn;
} break;
}
/* * This releases a chunk of memory and then does the identity map. It's used * as a fallback if the remapping fails.
*/ staticvoid __init xen_set_identity_and_release_chunk(unsignedlong start_pfn, unsignedlong end_pfn)
{ unsignedlong pfn, end; int ret;
/* Make sure pfn exists to start with */ if (mfn == INVALID_P2M_ENTRY || mfn_to_pfn(mfn) != pfn) continue;
ret = xen_free_mfn(mfn);
WARN(ret != 1, "Failed to release pfn %lx err=%d\n", pfn, ret);
if (ret == 1) {
xen_released_pages++; if (!__set_phys_to_machine(pfn, INVALID_P2M_ENTRY)) break;
} else break;
}
set_phys_range_identity(start_pfn, end_pfn);
}
/* * Helper function to update the p2m and m2p tables and kernel mapping.
*/ staticvoid __init xen_update_mem_tables(unsignedlong pfn, unsignedlong mfn)
{ struct mmu_update update = {
.ptr = ((uint64_t)mfn << PAGE_SHIFT) | MMU_MACHPHYS_UPDATE,
.val = pfn
};
/* Update p2m */ if (!set_phys_to_machine(pfn, mfn)) {
WARN(1, "Failed to set p2m mapping for pfn=%ld mfn=%ld\n",
pfn, mfn);
BUG();
}
/* Update m2p */ if (HYPERVISOR_mmu_update(&update, 1, NULL, DOMID_SELF) < 0) {
WARN(1, "Failed to set m2p mapping for mfn=%ld pfn=%ld\n",
mfn, pfn);
BUG();
}
if (HYPERVISOR_update_va_mapping((unsignedlong)__va(pfn << PAGE_SHIFT),
mfn_pte(mfn, PAGE_KERNEL), 0)) {
WARN(1, "Failed to update kernel mapping for mfn=%ld pfn=%ld\n",
mfn, pfn);
BUG();
}
}
/* * This function updates the p2m and m2p tables with an identity map from * start_pfn to start_pfn+size and prepares remapping the underlying RAM of the * original allocation at remap_pfn. The information needed for remapping is * saved in the memory itself to avoid the need for allocating buffers. The * complete remap information is contained in a list of MFNs each containing * up to REMAP_SIZE MFNs and the start target PFN for doing the remap. * This enables us to preserve the original mfn sequence while doing the * remapping at a time when the memory management is capable of allocating * virtual and physical memory in arbitrary amounts, see 'xen_remap_memory' and * its callers.
*/ staticvoid __init xen_do_set_identity_and_remap_chunk( unsignedlong start_pfn, unsignedlong size, unsignedlong remap_pfn)
{ unsignedlong buf = (unsignedlong)&xen_remap_buf; unsignedlong mfn_save, mfn; unsignedlong ident_pfn_iter, remap_pfn_iter; unsignedlong ident_end_pfn = start_pfn + size; unsignedlong left = size; unsignedint i, chunk;
/* Map first pfn to xen_remap_buf */
mfn = pfn_to_mfn(ident_pfn_iter);
set_pte_mfn(buf, mfn, PAGE_KERNEL);
/* Save mapping information in page */
xen_remap_buf.next_area_mfn = xen_remap_mfn;
xen_remap_buf.target_pfn = remap_pfn_iter;
xen_remap_buf.size = chunk; for (i = 0; i < chunk; i++)
xen_remap_buf.mfns[i] = pfn_to_mfn(ident_pfn_iter + i);
/* Put remap buf into list. */
xen_remap_mfn = mfn;
/* Set identity map */
set_phys_range_identity(ident_pfn_iter, ident_pfn_iter + chunk);
left -= chunk;
}
/* Restore old xen_remap_buf mapping */
set_pte_mfn(buf, mfn_save, PAGE_KERNEL);
}
/* * This function takes a contiguous pfn range that needs to be identity mapped * and: * * 1) Finds a new range of pfns to use to remap based on E820 and remap_pfn. * 2) Calls the do_ function to actually do the mapping/remapping work. * * The goal is to not allocate additional memory but to remap the existing * pages. In the case of an error the underlying memory is simply released back * to Xen and not remapped.
*/ staticunsignedlong __init xen_set_identity_and_remap_chunk( unsignedlong start_pfn, unsignedlong end_pfn, unsignedlong remap_pfn)
{ unsignedlong pfn; unsignedlong i = 0; unsignedlong n = end_pfn - start_pfn;
if (remap_pfn == 0)
remap_pfn = ini_nr_pages;
while (i < n) { unsignedlong cur_pfn = start_pfn + i; unsignedlong left = n - i; unsignedlong size = left; unsignedlong remap_range_size;
/* Do not remap pages beyond the current allocation */ if (cur_pfn >= ini_nr_pages) { /* Identity map remaining pages */
set_phys_range_identity(cur_pfn, cur_pfn + size); break;
} if (cur_pfn + size > ini_nr_pages)
size = ini_nr_pages - cur_pfn;
remap_range_size = xen_find_pfn_range(&remap_pfn); if (!remap_range_size) {
pr_warn("Unable to find available pfn range, not remapping identity pages\n");
xen_set_identity_and_release_chunk(cur_pfn,
cur_pfn + left); break;
} /* Adjust size to fit in current e820 RAM region */ if (size > remap_range_size)
size = remap_range_size;
/* Update variables to reflect new mappings. */
i += size;
remap_pfn += size;
}
/* * If the PFNs are currently mapped, their VA mappings need to be * zapped.
*/ for (pfn = start_pfn; pfn <= max_pfn_mapped && pfn < end_pfn; pfn++)
(void)HYPERVISOR_update_va_mapping(
(unsignedlong)__va(pfn << PAGE_SHIFT),
native_make_pte(0), 0);
/* * Combine non-RAM regions and gaps until a RAM region (or the * end of the map) is reached, then call the provided function * to perform its duty on the non-RAM region. * * The combined non-RAM regions are rounded to a whole number * of pages so any partial pages are accessible via the 1:1 * mapping. This is needed for some BIOSes that put (for * example) the DMI tables in a reserved region that begins on * a non-page boundary.
*/ for (i = 0; i < xen_e820_table.nr_entries; i++, entry++) {
phys_addr_t end = entry->addr + entry->size; if (entry->type == E820_TYPE_RAM || i == xen_e820_table.nr_entries - 1) { unsignedlong start_pfn = PFN_DOWN(start); unsignedlong end_pfn = PFN_UP(end);
if (entry->type == E820_TYPE_RAM)
end_pfn = PFN_UP(entry->addr);
/* * Remap the memory prepared in xen_do_set_identity_and_remap_chunk(). * The remap information (which mfn remap to which pfn) is contained in the * to be remapped memory itself in a linked list anchored at xen_remap_mfn. * This scheme allows to remap the different chunks in arbitrary order while * the resulting mapping will be independent from the order.
*/ void __init xen_remap_memory(void)
{ unsignedlong buf = (unsignedlong)&xen_remap_buf; unsignedlong mfn_save, pfn; unsignedlong remapped = 0; unsignedint i; unsignedlong pfn_s = ~0UL; unsignedlong len = 0;
mfn_save = virt_to_mfn((void *)buf);
while (xen_remap_mfn != INVALID_P2M_ENTRY) { /* Map the remap information */
set_pte_mfn(buf, xen_remap_mfn, PAGE_KERNEL);
BUG_ON(xen_remap_mfn != xen_remap_buf.mfns[0]);
pfn = xen_remap_buf.target_pfn; for (i = 0; i < xen_remap_buf.size; i++) {
xen_update_mem_tables(pfn, xen_remap_buf.mfns[i]);
remapped++;
pfn++;
} if (pfn_s == ~0UL || pfn == pfn_s) {
pfn_s = xen_remap_buf.target_pfn;
len += xen_remap_buf.size;
} elseif (pfn_s + len == xen_remap_buf.target_pfn) {
len += xen_remap_buf.size;
} else {
xen_del_extra_mem(pfn_s, len);
pfn_s = xen_remap_buf.target_pfn;
len = xen_remap_buf.size;
}
xen_remap_mfn = xen_remap_buf.next_area_mfn;
}
if (pfn_s != ~0UL && len)
xen_del_extra_mem(pfn_s, len);
/* * For the initial domain we use the maximum reservation as * the maximum page. * * For guest domains the current maximum reservation reflects * the current maximum rather than the static maximum. In this * case the e820 map provided to us will cover the static * maximum region.
*/ if (xen_initial_domain()) {
ret = HYPERVISOR_memory_op(XENMEM_maximum_reservation, &domid); if (ret > 0)
max_pages = ret;
}
return min(max_pages, limit);
}
staticvoid __init xen_align_and_add_e820_region(phys_addr_t start,
phys_addr_t size, int type)
{
phys_addr_t end = start + size;
/* Align RAM regions to page boundaries. */ if (type == E820_TYPE_RAM) {
start = PAGE_ALIGN(start);
end &= ~((phys_addr_t)PAGE_SIZE - 1); #ifdef CONFIG_MEMORY_HOTPLUG /* * Don't allow adding memory not in E820 map while booting the * system. Once the balloon driver is up it will remove that * restriction again.
*/
max_mem_size = end; #endif
}
/* * Find a free area in physical memory not yet reserved and compliant with * E820 map. * Used to relocate pre-allocated areas like initrd or p2m list which are in * conflict with the to be used E820 map. * In case no area is found, return 0. Otherwise return the physical address * of the area which is already reserved for convenience.
*/
phys_addr_t __init xen_find_free_area(phys_addr_t size)
{ unsigned mapcnt;
phys_addr_t addr, start; struct e820_entry *entry = xen_e820_table.entries;
/* * Swap a non-RAM E820 map entry with RAM above ini_nr_pages. * Note that the E820 map is modified accordingly, but the P2M map isn't yet. * The adaption of the P2M must be deferred until page allocation is possible.
*/ staticvoid __init xen_e820_swap_entry_with_ram(struct e820_entry *swap_entry)
{ struct e820_entry *entry; unsignedint mapcnt;
phys_addr_t mem_end = PFN_PHYS(ini_nr_pages);
phys_addr_t swap_addr, swap_size, entry_end;
/* Order E820 table and merge entries. */
e820__update_table(&xen_e820_table);
return;
}
entry++;
}
xen_raw_console_write("No suitable area found for required E820 entry remapping action\n");
BUG();
}
/* * Look for non-RAM memory types in a specific guest physical area and move * those away if possible (ACPI NVS only for now).
*/ staticvoid __init xen_e820_resolve_conflicts(phys_addr_t start,
phys_addr_t size)
{ struct e820_entry *entry; unsignedint mapcnt;
phys_addr_t end;
if (!size) return;
end = start + size;
entry = xen_e820_table.entries;
for (mapcnt = 0; mapcnt < xen_e820_table.nr_entries; mapcnt++) { if (entry->addr >= end) return;
/* * Check for an area in physical memory to be usable for non-movable purposes. * An area is considered to usable if the used E820 map lists it to be RAM or * some other type which can be moved to higher PFNs while keeping the MFNs. * In case the area is not usable, crash the system with an error message.
*/ void __init xen_chk_is_e820_usable(phys_addr_t start, phys_addr_t size, constchar *component)
{
xen_e820_resolve_conflicts(start, size);
if (xen_initial_domain()) { /* * Xen won't allow a 1:1 mapping to be created to UNUSABLE * regions, so if we're using the machine memory map leave the * region as RAM as it is in the pseudo-physical map. * * UNUSABLE regions in domUs are not handled and will need * a patch in the future.
*/
xen_ignore_unusable();
#ifdef CONFIG_ISCSI_IBFT_FIND /* Reserve 0.5 MiB to 1 MiB region so iBFT can be found */
xen_e820_table.entries[xen_e820_table.nr_entries].addr = IBFT_START;
xen_e820_table.entries[xen_e820_table.nr_entries].size = IBFT_END - IBFT_START;
xen_e820_table.entries[xen_e820_table.nr_entries].type = E820_TYPE_RESERVED;
xen_e820_table.nr_entries++; #endif
}
/* Make sure the Xen-supplied memory map is well-ordered. */
e820__update_table(&xen_e820_table);
/* * Check whether the kernel itself conflicts with the target E820 map. * Failing now is better than running into weird problems later due * to relocating (and even reusing) pages with kernel text or data.
*/
xen_chk_is_e820_usable(__pa_symbol(_text),
__pa_symbol(_end) - __pa_symbol(_text), "kernel");
/* * Check for a conflict of the xen_start_info memory with the target * E820 map.
*/
xen_chk_is_e820_usable(__pa(xen_start_info), sizeof(*xen_start_info), "xen_start_info");
/* * Check for a conflict of the hypervisor supplied page tables with * the target E820 map.
*/
xen_pt_check_e820();
max_pages = xen_get_max_pages();
/* How many extra pages do we need due to remapping? */
max_pages += xen_foreach_remap_area(xen_count_remap_pages);
if (max_pages > ini_nr_pages)
extra_pages += max_pages - ini_nr_pages;
/* * Clamp the amount of extra memory to a EXTRA_MEM_RATIO * factor the base size. * * Make sure we have no memory above max_pages, as this area * isn't handled by the p2m management.
*/
maxmem_pages = EXTRA_MEM_RATIO * min(ini_nr_pages, PFN_DOWN(MAXMEM));
extra_pages = min3(maxmem_pages, extra_pages, max_pages - ini_nr_pages);
i = 0;
addr = xen_e820_table.entries[0].addr;
size = xen_e820_table.entries[0].size; while (i < xen_e820_table.nr_entries) { bool discard = false;
chunk_size = size;
type = xen_e820_table.entries[i].type;
if (type == E820_TYPE_RESERVED)
xen_pv_pci_possible = true;
if (!discard)
xen_align_and_add_e820_region(addr, chunk_size, type);
addr += chunk_size;
size -= chunk_size; if (size == 0) {
i++; if (i < xen_e820_table.nr_entries) {
addr = xen_e820_table.entries[i].addr;
size = xen_e820_table.entries[i].size;
}
}
}
/* * Set the rest as identity mapped, in case PCI BARs are * located here.
*/
set_phys_range_identity(addr / PAGE_SIZE, ~0ul);
/* * In domU, the ISA region is normal, usable memory, but we * reserve ISA memory anyway because too many things poke * about in there.
*/
e820__range_add(ISA_START_ADDRESS, ISA_END_ADDRESS - ISA_START_ADDRESS, E820_TYPE_RESERVED);
e820__update_table(e820_table);
xen_reserve_xen_mfnlist();
/* Check for a conflict of the initrd with the target E820 map. */ if (xen_is_e820_reserved(boot_params.hdr.ramdisk_image,
boot_params.hdr.ramdisk_size)) {
phys_addr_t new_area, start, size;
new_area = xen_find_free_area(boot_params.hdr.ramdisk_size); if (!new_area) {
xen_raw_console_write("Can't find new memory area for initrd needed due to E820 map conflict\n");
BUG();
}
void xen_enable_sysenter(void)
{ if (cpu_feature_enabled(X86_FEATURE_SYSENTER32) &&
register_callback(CALLBACKTYPE_sysenter, xen_entry_SYSENTER_compat))
setup_clear_cpu_cap(X86_FEATURE_SYSENTER32);
}
void xen_enable_syscall(void)
{ int ret;
ret = register_callback(CALLBACKTYPE_syscall, xen_entry_SYSCALL_64); if (ret != 0) {
printk(KERN_ERR "Failed to set syscall callback: %d\n", ret); /* Pretty fatal; 64-bit userspace has no other
mechanism for syscalls. */
}
if (cpu_feature_enabled(X86_FEATURE_SYSCALL32) &&
register_callback(CALLBACKTYPE_syscall32, xen_entry_SYSCALL_compat))
setup_clear_cpu_cap(X86_FEATURE_SYSCALL32);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.