blob: e7c7b53a1435bacc8a118cfc2f7d1499534ed501 [file] [log] [blame]
Alex Dewar0d1fb0a2019-08-25 10:49:17 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Jeff Dike6d536e42007-10-16 01:26:47 -07003 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
Al Viro73395a02011-08-18 20:14:10 +01006#include <linux/module.h>
Mike Rapoportddf63982018-10-26 15:05:02 -07007#include <linux/memblock.h>
Al Viro73395a02011-08-18 20:14:10 +01008#include <linux/mm.h>
9#include <linux/pfn.h>
10#include <asm/page.h>
Nicolas Ioossd5f20be2014-10-12 13:02:11 +020011#include <asm/sections.h>
Al Viro73395a02011-08-18 20:14:10 +010012#include <as-layout.h>
13#include <init.h>
14#include <kern.h>
15#include <mem_user.h>
16#include <os.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018static int physmem_fd = -1;
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020/* Changed during early boot */
21unsigned long high_physmem;
Al Viro73395a02011-08-18 20:14:10 +010022EXPORT_SYMBOL(high_physmem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Jeff Dikeae173812005-11-07 00:58:57 -080024extern unsigned long long physmem_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Honggang Li9e6a57d2014-06-03 13:30:45 +080026void __init mem_total_pages(unsigned long physmem, unsigned long iomem,
Jeff Dike97a1fcb2007-07-23 18:43:48 -070027 unsigned long highmem)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Honggang Li9e6a57d2014-06-03 13:30:45 +080029 unsigned long phys_pages, highmem_pages;
30 unsigned long iomem_pages, total_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Honggang Li9e6a57d2014-06-03 13:30:45 +080032 phys_pages = physmem >> PAGE_SHIFT;
33 iomem_pages = iomem >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 highmem_pages = highmem >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Honggang Li9e6a57d2014-06-03 13:30:45 +080036 total_pages = phys_pages + iomem_pages + highmem_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 max_mapnr = total_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041void map_memory(unsigned long virt, unsigned long phys, unsigned long len,
42 int r, int w, int x)
43{
44 __u64 offset;
45 int fd, err;
46
47 fd = phys_mapping(phys, &offset);
48 err = os_map_memory((void *) virt, fd, offset, len, r, w, x);
Jeff Dike6d536e42007-10-16 01:26:47 -070049 if (err) {
50 if (err == -ENOMEM)
Jeff Dikeba180fd2007-10-16 01:27:00 -070051 printk(KERN_ERR "try increasing the host's "
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 "/proc/sys/vm/max_map_count to <physical "
53 "memory size>/4096\n");
54 panic("map_memory(0x%lx, %d, 0x%llx, %ld, %d, %d, %d) failed, "
55 "err = %d\n", virt, fd, offset, len, r, w, x, err);
56 }
57}
58
Thomas Meyerfe205bd2015-04-03 16:51:26 +020059/**
60 * setup_physmem() - Setup physical memory for UML
61 * @start: Start address of the physical kernel memory,
62 * i.e start address of the executable image.
63 * @reserve_end: end address of the physical kernel memory.
64 * @len: Length of total physical memory that should be mapped/made
65 * available, in bytes.
66 * @highmem: Number of highmem bytes that should be mapped/made available.
67 *
68 * Creates an unlinked temporary file of size (len + highmem) and memory maps
69 * it on the last executable image address (uml_reserved).
70 *
71 * The offset is needed as the length of the total physical memory
72 * (len + highmem) includes the size of the memory used be the executable image,
73 * but the mapped-to address is the last address of the executable image
74 * (uml_reserved == end address of executable image).
75 *
76 * The memory mapped memory of the temporary file is used as backing memory
77 * of all user space processes/kernel tasks.
78 */
Jeff Dike97a1fcb2007-07-23 18:43:48 -070079void __init setup_physmem(unsigned long start, unsigned long reserve_end,
80 unsigned long len, unsigned long long highmem)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 unsigned long reserve = reserve_end - start;
Mike Rapoportddf63982018-10-26 15:05:02 -070083 long map_size = len - reserve;
Thomas Meyerfe205bd2015-04-03 16:51:26 +020084 int err;
85
Thomas Meyerfe205bd2015-04-03 16:51:26 +020086 if(map_size <= 0) {
Masami Hiramatsu0936d4f2017-05-18 02:19:31 +090087 os_warn("Too few physical memory! Needed=%lu, given=%lu\n",
Mike Rapoportddf63982018-10-26 15:05:02 -070088 reserve, len);
Thomas Meyerfe205bd2015-04-03 16:51:26 +020089 exit(1);
90 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 physmem_fd = create_mem_file(len + highmem);
93
Mike Rapoportddf63982018-10-26 15:05:02 -070094 err = os_map_memory((void *) reserve_end, physmem_fd, reserve,
Thomas Meyerfe205bd2015-04-03 16:51:26 +020095 map_size, 1, 1, 1);
Jeff Dike6d536e42007-10-16 01:26:47 -070096 if (err < 0) {
Masami Hiramatsu0936d4f2017-05-18 02:19:31 +090097 os_warn("setup_physmem - mapping %ld bytes of memory at 0x%p "
98 "failed - errno = %d\n", map_size,
Mike Rapoportbe6ec5b2018-10-26 15:04:58 -070099 (void *) reserve_end, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 exit(1);
101 }
102
Jeff Dikeba180fd2007-10-16 01:27:00 -0700103 /*
104 * Special kludge - This page will be mapped in to userspace processes
Jeff Diked67b5692005-07-07 17:56:49 -0700105 * from physmem_fd, so it needs to be written out there.
106 */
Nicolas Iooss05eacfd2014-10-12 13:02:12 +0200107 os_seek_file(physmem_fd, __pa(__syscall_stub_start));
108 os_write_file(physmem_fd, __syscall_stub_start, PAGE_SIZE);
Anton Ivanov05651032014-03-07 18:37:47 +0000109 os_fsync_file(physmem_fd);
Jeff Diked67b5692005-07-07 17:56:49 -0700110
Mike Rapoportddf63982018-10-26 15:05:02 -0700111 memblock_add(__pa(start), len + highmem);
112 memblock_reserve(__pa(start), reserve);
113
114 min_low_pfn = PFN_UP(__pa(reserve_end));
115 max_low_pfn = min_low_pfn + (map_size >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Jeff Dike0a7675a2007-10-16 01:27:05 -0700118int phys_mapping(unsigned long phys, unsigned long long *offset_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 int fd = -1;
121
Jeff Dike6d536e42007-10-16 01:26:47 -0700122 if (phys < physmem_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 fd = physmem_fd;
124 *offset_out = phys;
125 }
Jeff Dike6d536e42007-10-16 01:26:47 -0700126 else if (phys < __pa(end_iomem)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 struct iomem_region *region = iomem_regions;
128
Jeff Dike6d536e42007-10-16 01:26:47 -0700129 while (region != NULL) {
130 if ((phys >= region->phys) &&
131 (phys < region->phys + region->size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 fd = region->fd;
133 *offset_out = phys - region->phys;
134 break;
135 }
136 region = region->next;
137 }
138 }
Jeff Dike6d536e42007-10-16 01:26:47 -0700139 else if (phys < __pa(end_iomem) + highmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 fd = physmem_fd;
141 *offset_out = phys - iomem_size;
142 }
143
Jeff Dike60678bb2007-02-10 01:44:10 -0800144 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
Erel Geron5d38f322019-09-11 14:51:20 +0200146EXPORT_SYMBOL(phys_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148static int __init uml_mem_setup(char *line, int *add)
149{
150 char *retptr;
151 physmem_size = memparse(line,&retptr);
152 return 0;
153}
154__uml_setup("mem=", uml_mem_setup,
155"mem=<Amount of desired ram>\n"
156" This controls how much \"physical\" memory the kernel allocates\n"
157" for the system. The size is specified as a number followed by\n"
158" one of 'k', 'K', 'm', 'M', which have the obvious meanings.\n"
159" This is not related to the amount of memory in the host. It can\n"
160" be more, and the excess, if it's ever used, will just be swapped out.\n"
161" Example: mem=64M\n\n"
162);
163
Jeff Dike94c282d2007-02-10 01:44:09 -0800164extern int __init parse_iomem(char *str, int *add);
165
166__uml_setup("iomem=", parse_iomem,
167"iomem=<name>,<file>\n"
168" Configure <file> as an IO memory region named <name>.\n\n"
169);
170
171/*
172 * This list is constructed in parse_iomem and addresses filled in in
173 * setup_iomem, both of which run during early boot. Afterwards, it's
174 * unchanged.
175 */
Jeff Dike80e39312008-02-04 22:31:17 -0800176struct iomem_region *iomem_regions;
Jeff Dike94c282d2007-02-10 01:44:09 -0800177
Jeff Dike80e39312008-02-04 22:31:17 -0800178/* Initialized in parse_iomem and unchanged thereafter */
179int iomem_size;
Jeff Dike94c282d2007-02-10 01:44:09 -0800180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181unsigned long find_iomem(char *driver, unsigned long *len_out)
182{
183 struct iomem_region *region = iomem_regions;
184
Jeff Dike6d536e42007-10-16 01:26:47 -0700185 while (region != NULL) {
186 if (!strcmp(region->driver, driver)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 *len_out = region->size;
Jeff Dike60678bb2007-02-10 01:44:10 -0800188 return region->virt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
Victor V. Vengerovc39e50b2006-05-01 12:15:53 -0700190
191 region = region->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193
Jeff Dike60678bb2007-02-10 01:44:10 -0800194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
Al Viro73395a02011-08-18 20:14:10 +0100196EXPORT_SYMBOL(find_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
WANG Cong99764fa2008-07-23 21:28:49 -0700198static int setup_iomem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 struct iomem_region *region = iomem_regions;
201 unsigned long iomem_start = high_physmem + PAGE_SIZE;
202 int err;
203
Jeff Dike6d536e42007-10-16 01:26:47 -0700204 while (region != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 err = os_map_memory((void *) iomem_start, region->fd, 0,
206 region->size, 1, 1, 0);
Jeff Dike6d536e42007-10-16 01:26:47 -0700207 if (err)
Jeff Dikeba180fd2007-10-16 01:27:00 -0700208 printk(KERN_ERR "Mapping iomem region for driver '%s' "
209 "failed, errno = %d\n", region->driver, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 else {
211 region->virt = iomem_start;
212 region->phys = __pa(region->virt);
213 }
214
215 iomem_start += region->size + PAGE_SIZE;
216 region = region->next;
217 }
218
Jeff Dike60678bb2007-02-10 01:44:10 -0800219 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222__initcall(setup_iomem);