2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
6 #include <linux/module.h>
7 #include <linux/bootmem.h>
11 #include <asm/sections.h>
12 #include <as-layout.h>
18 static int physmem_fd
= -1;
20 /* Changed during early boot */
21 unsigned long high_physmem
;
22 EXPORT_SYMBOL(high_physmem
);
24 extern unsigned long long physmem_size
;
26 void __init
mem_total_pages(unsigned long physmem
, unsigned long iomem
,
27 unsigned long highmem
)
29 unsigned long phys_pages
, highmem_pages
;
30 unsigned long iomem_pages
, total_pages
;
32 phys_pages
= physmem
>> PAGE_SHIFT
;
33 iomem_pages
= iomem
>> PAGE_SHIFT
;
34 highmem_pages
= highmem
>> PAGE_SHIFT
;
36 total_pages
= phys_pages
+ iomem_pages
+ highmem_pages
;
38 max_mapnr
= total_pages
;
41 void map_memory(unsigned long virt
, unsigned long phys
, unsigned long len
,
47 fd
= phys_mapping(phys
, &offset
);
48 err
= os_map_memory((void *) virt
, fd
, offset
, len
, r
, w
, x
);
51 printk(KERN_ERR
"try increasing the host's "
52 "/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
);
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.
68 * Creates an unlinked temporary file of size (len + highmem) and memory maps
69 * it on the last executable image address (uml_reserved).
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).
76 * The memory mapped memory of the temporary file is used as backing memory
77 * of all user space processes/kernel tasks.
79 void __init
setup_physmem(unsigned long start
, unsigned long reserve_end
,
80 unsigned long len
, unsigned long long highmem
)
82 unsigned long reserve
= reserve_end
- start
;
83 unsigned long pfn
= PFN_UP(__pa(reserve_end
));
84 unsigned long delta
= (len
- reserve
) >> PAGE_SHIFT
;
85 unsigned long offset
, bootmap_size
;
89 offset
= uml_reserved
- uml_physmem
;
90 map_size
= len
- offset
;
92 os_warn("Too few physical memory! Needed=%lu, given=%lu\n",
97 physmem_fd
= create_mem_file(len
+ highmem
);
99 err
= os_map_memory((void *) uml_reserved
, physmem_fd
, offset
,
102 os_warn("setup_physmem - mapping %ld bytes of memory at 0x%p "
103 "failed - errno = %d\n", map_size
,
104 (void *) uml_reserved
, err
);
109 * Special kludge - This page will be mapped in to userspace processes
110 * from physmem_fd, so it needs to be written out there.
112 os_seek_file(physmem_fd
, __pa(__syscall_stub_start
));
113 os_write_file(physmem_fd
, __syscall_stub_start
, PAGE_SIZE
);
114 os_fsync_file(physmem_fd
);
116 bootmap_size
= init_bootmem(pfn
, pfn
+ delta
);
117 free_bootmem(__pa(reserve_end
) + bootmap_size
,
118 len
- bootmap_size
- reserve
);
121 int phys_mapping(unsigned long phys
, unsigned long long *offset_out
)
125 if (phys
< physmem_size
) {
129 else if (phys
< __pa(end_iomem
)) {
130 struct iomem_region
*region
= iomem_regions
;
132 while (region
!= NULL
) {
133 if ((phys
>= region
->phys
) &&
134 (phys
< region
->phys
+ region
->size
)) {
136 *offset_out
= phys
- region
->phys
;
139 region
= region
->next
;
142 else if (phys
< __pa(end_iomem
) + highmem
) {
144 *offset_out
= phys
- iomem_size
;
150 static int __init
uml_mem_setup(char *line
, int *add
)
153 physmem_size
= memparse(line
,&retptr
);
156 __uml_setup("mem=", uml_mem_setup
,
157 "mem=<Amount of desired ram>\n"
158 " This controls how much \"physical\" memory the kernel allocates\n"
159 " for the system. The size is specified as a number followed by\n"
160 " one of 'k', 'K', 'm', 'M', which have the obvious meanings.\n"
161 " This is not related to the amount of memory in the host. It can\n"
162 " be more, and the excess, if it's ever used, will just be swapped out.\n"
163 " Example: mem=64M\n\n"
166 extern int __init
parse_iomem(char *str
, int *add
);
168 __uml_setup("iomem=", parse_iomem
,
169 "iomem=<name>,<file>\n"
170 " Configure <file> as an IO memory region named <name>.\n\n"
174 * This list is constructed in parse_iomem and addresses filled in in
175 * setup_iomem, both of which run during early boot. Afterwards, it's
178 struct iomem_region
*iomem_regions
;
180 /* Initialized in parse_iomem and unchanged thereafter */
183 unsigned long find_iomem(char *driver
, unsigned long *len_out
)
185 struct iomem_region
*region
= iomem_regions
;
187 while (region
!= NULL
) {
188 if (!strcmp(region
->driver
, driver
)) {
189 *len_out
= region
->size
;
193 region
= region
->next
;
198 EXPORT_SYMBOL(find_iomem
);
200 static int setup_iomem(void)
202 struct iomem_region
*region
= iomem_regions
;
203 unsigned long iomem_start
= high_physmem
+ PAGE_SIZE
;
206 while (region
!= NULL
) {
207 err
= os_map_memory((void *) iomem_start
, region
->fd
, 0,
208 region
->size
, 1, 1, 0);
210 printk(KERN_ERR
"Mapping iomem region for driver '%s' "
211 "failed, errno = %d\n", region
->driver
, -err
);
213 region
->virt
= iomem_start
;
214 region
->phys
= __pa(region
->virt
);
217 iomem_start
+= region
->size
+ PAGE_SIZE
;
218 region
= region
->next
;
224 __initcall(setup_iomem
);