^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/pfn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <asm/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <as-layout.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <kern.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <mem_user.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <os.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static int physmem_fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /* Changed during early boot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned long high_physmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) EXPORT_SYMBOL(high_physmem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) extern unsigned long long physmem_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) void __init mem_total_pages(unsigned long physmem, unsigned long iomem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) unsigned long highmem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) unsigned long phys_pages, highmem_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) unsigned long iomem_pages, total_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) phys_pages = physmem >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) iomem_pages = iomem >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) highmem_pages = highmem >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) total_pages = phys_pages + iomem_pages + highmem_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) max_mapnr = total_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) void map_memory(unsigned long virt, unsigned long phys, unsigned long len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int r, int w, int x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) __u64 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int fd, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) fd = phys_mapping(phys, &offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) err = os_map_memory((void *) virt, fd, offset, len, r, w, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (err == -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) printk(KERN_ERR "try increasing the host's "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) "/proc/sys/vm/max_map_count to <physical "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) "memory size>/4096\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) panic("map_memory(0x%lx, %d, 0x%llx, %ld, %d, %d, %d) failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) "err = %d\n", virt, fd, offset, len, r, w, x, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * setup_physmem() - Setup physical memory for UML
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * @start: Start address of the physical kernel memory,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * i.e start address of the executable image.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * @reserve_end: end address of the physical kernel memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * @len: Length of total physical memory that should be mapped/made
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * available, in bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * @highmem: Number of highmem bytes that should be mapped/made available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * Creates an unlinked temporary file of size (len + highmem) and memory maps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * it on the last executable image address (uml_reserved).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * The offset is needed as the length of the total physical memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * (len + highmem) includes the size of the memory used be the executable image,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * but the mapped-to address is the last address of the executable image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * (uml_reserved == end address of executable image).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * The memory mapped memory of the temporary file is used as backing memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * of all user space processes/kernel tasks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) void __init setup_physmem(unsigned long start, unsigned long reserve_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) unsigned long len, unsigned long long highmem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) unsigned long reserve = reserve_end - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) long map_size = len - reserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if(map_size <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) os_warn("Too few physical memory! Needed=%lu, given=%lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) reserve, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) physmem_fd = create_mem_file(len + highmem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) err = os_map_memory((void *) reserve_end, physmem_fd, reserve,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) map_size, 1, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) os_warn("setup_physmem - mapping %ld bytes of memory at 0x%p "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) "failed - errno = %d\n", map_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) (void *) reserve_end, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * Special kludge - This page will be mapped in to userspace processes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * from physmem_fd, so it needs to be written out there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) os_seek_file(physmem_fd, __pa(__syscall_stub_start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) os_write_file(physmem_fd, __syscall_stub_start, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) os_fsync_file(physmem_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) memblock_add(__pa(start), len + highmem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) memblock_reserve(__pa(start), reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) min_low_pfn = PFN_UP(__pa(reserve_end));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) max_low_pfn = min_low_pfn + (map_size >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) int phys_mapping(unsigned long phys, unsigned long long *offset_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (phys < physmem_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) fd = physmem_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) *offset_out = phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) else if (phys < __pa(end_iomem)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct iomem_region *region = iomem_regions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) while (region != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if ((phys >= region->phys) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) (phys < region->phys + region->size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) fd = region->fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) *offset_out = phys - region->phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) region = region->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) else if (phys < __pa(end_iomem) + highmem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) fd = physmem_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) *offset_out = phys - iomem_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) EXPORT_SYMBOL(phys_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static int __init uml_mem_setup(char *line, int *add)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) char *retptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) physmem_size = memparse(line,&retptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) __uml_setup("mem=", uml_mem_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) "mem=<Amount of desired ram>\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) " This controls how much \"physical\" memory the kernel allocates\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) " for the system. The size is specified as a number followed by\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) " one of 'k', 'K', 'm', 'M', which have the obvious meanings.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) " This is not related to the amount of memory in the host. It can\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) " be more, and the excess, if it's ever used, will just be swapped out.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) " Example: mem=64M\n\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) extern int __init parse_iomem(char *str, int *add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) __uml_setup("iomem=", parse_iomem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) "iomem=<name>,<file>\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) " Configure <file> as an IO memory region named <name>.\n\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * This list is constructed in parse_iomem and addresses filled in in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * setup_iomem, both of which run during early boot. Afterwards, it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * unchanged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct iomem_region *iomem_regions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /* Initialized in parse_iomem and unchanged thereafter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) int iomem_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) unsigned long find_iomem(char *driver, unsigned long *len_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct iomem_region *region = iomem_regions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) while (region != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (!strcmp(region->driver, driver)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) *len_out = region->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return region->virt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) region = region->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) EXPORT_SYMBOL(find_iomem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static int setup_iomem(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct iomem_region *region = iomem_regions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) unsigned long iomem_start = high_physmem + PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) while (region != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) err = os_map_memory((void *) iomem_start, region->fd, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) region->size, 1, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) printk(KERN_ERR "Mapping iomem region for driver '%s' "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) "failed, errno = %d\n", region->driver, -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) region->virt = iomem_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) region->phys = __pa(region->virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) iomem_start += region->size + PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) region = region->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) __initcall(setup_iomem);