2 * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
7 #include "linux/rbtree.h"
8 #include "linux/slab.h"
9 #include "linux/vmalloc.h"
10 #include "linux/bootmem.h"
11 #include "linux/module.h"
12 #include "linux/pfn.h"
13 #include "asm/types.h"
14 #include "asm/pgtable.h"
15 #include "kern_util.h"
16 #include "user_util.h"
17 #include "mode_kern.h"
30 struct list_head list;
33 static struct rb_root phys_mappings = RB_ROOT;
35 static struct rb_node **find_rb(void *virt)
37 struct rb_node **n = &phys_mappings.rb_node;
41 d = rb_entry(*n, struct phys_desc, rb);
54 static struct phys_desc *find_phys_mapping(void *virt)
56 struct rb_node **n = find_rb(virt);
61 return(rb_entry(*n, struct phys_desc, rb));
64 static void insert_phys_mapping(struct phys_desc *desc)
66 struct rb_node **n = find_rb(desc->virt);
69 panic("Physical remapping for %p already present",
72 rb_link_node(&desc->rb, rb_parent(*n), n);
73 rb_insert_color(&desc->rb, &phys_mappings);
76 LIST_HEAD(descriptor_mappings);
80 struct list_head list;
81 struct list_head pages;
84 static struct desc_mapping *find_mapping(int fd)
86 struct desc_mapping *desc;
87 struct list_head *ele;
89 list_for_each(ele, &descriptor_mappings){
90 desc = list_entry(ele, struct desc_mapping, list);
98 static struct desc_mapping *descriptor_mapping(int fd)
100 struct desc_mapping *desc;
102 desc = find_mapping(fd);
106 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
110 *desc = ((struct desc_mapping)
112 .list = LIST_HEAD_INIT(desc->list),
113 .pages = LIST_HEAD_INIT(desc->pages) });
114 list_add(&desc->list, &descriptor_mappings);
119 int physmem_subst_mapping(void *virt, int fd, __u64 offset, int w)
121 struct desc_mapping *fd_maps;
122 struct phys_desc *desc;
126 fd_maps = descriptor_mapping(fd);
131 desc = find_phys_mapping(virt);
133 panic("Address 0x%p is already substituted\n", virt);
136 desc = kmalloc(sizeof(*desc), GFP_ATOMIC);
140 *desc = ((struct phys_desc)
145 .list = LIST_HEAD_INIT(desc->list) });
146 insert_phys_mapping(desc);
148 list_add(&desc->list, &fd_maps->pages);
150 virt = (void *) ((unsigned long) virt & PAGE_MASK);
151 err = os_map_memory(virt, fd, offset, PAGE_SIZE, 1, w, 0);
155 rb_erase(&desc->rb, &phys_mappings);
161 static int physmem_fd = -1;
163 static void remove_mapping(struct phys_desc *desc)
165 void *virt = desc->virt;
168 rb_erase(&desc->rb, &phys_mappings);
169 list_del(&desc->list);
172 err = os_map_memory(virt, physmem_fd, __pa(virt), PAGE_SIZE, 1, 1, 0);
174 panic("Failed to unmap block device page from physical memory, "
178 int physmem_remove_mapping(void *virt)
180 struct phys_desc *desc;
182 virt = (void *) ((unsigned long) virt & PAGE_MASK);
183 desc = find_phys_mapping(virt);
187 remove_mapping(desc);
191 void physmem_forget_descriptor(int fd)
193 struct desc_mapping *desc;
194 struct phys_desc *page;
195 struct list_head *ele, *next;
200 desc = find_mapping(fd);
204 list_for_each_safe(ele, next, &desc->pages){
205 page = list_entry(ele, struct phys_desc, list);
206 offset = page->offset;
208 remove_mapping(page);
209 err = os_seek_file(fd, offset);
211 panic("physmem_forget_descriptor - failed to seek "
212 "to %lld in fd %d, error = %d\n",
214 err = os_read_file(fd, addr, PAGE_SIZE);
216 panic("physmem_forget_descriptor - failed to read "
217 "from fd %d to 0x%p, error = %d\n",
221 list_del(&desc->list);
225 EXPORT_SYMBOL(physmem_forget_descriptor);
226 EXPORT_SYMBOL(physmem_remove_mapping);
227 EXPORT_SYMBOL(physmem_subst_mapping);
229 void arch_free_page(struct page *page, int order)
234 for(i = 0; i < (1 << order); i++){
235 virt = __va(page_to_phys(page + i));
236 physmem_remove_mapping(virt);
240 int is_remapped(void *virt)
242 struct phys_desc *desc = find_phys_mapping(virt);
244 return(desc != NULL);
247 /* Changed during early boot */
248 unsigned long high_physmem;
250 extern unsigned long long physmem_size;
252 int init_maps(unsigned long physmem, unsigned long iomem, unsigned long highmem)
254 struct page *p, *map;
255 unsigned long phys_len, phys_pages, highmem_len, highmem_pages;
256 unsigned long iomem_len, iomem_pages, total_len, total_pages;
259 phys_pages = physmem >> PAGE_SHIFT;
260 phys_len = phys_pages * sizeof(struct page);
262 iomem_pages = iomem >> PAGE_SHIFT;
263 iomem_len = iomem_pages * sizeof(struct page);
265 highmem_pages = highmem >> PAGE_SHIFT;
266 highmem_len = highmem_pages * sizeof(struct page);
268 total_pages = phys_pages + iomem_pages + highmem_pages;
269 total_len = phys_len + iomem_len + highmem_len;
272 map = kmalloc(total_len, GFP_KERNEL);
274 map = vmalloc(total_len);
276 else map = alloc_bootmem_low_pages(total_len);
281 for(i = 0; i < total_pages; i++){
283 memset(p, 0, sizeof(struct page));
285 INIT_LIST_HEAD(&p->lru);
288 max_mapnr = total_pages;
292 /* Changed during early boot */
293 static unsigned long kmem_top = 0;
295 unsigned long get_kmem_end(void)
298 kmem_top = CHOOSE_MODE(kmem_end_tt, kmem_end_skas);
302 void map_memory(unsigned long virt, unsigned long phys, unsigned long len,
308 fd = phys_mapping(phys, &offset);
309 err = os_map_memory((void *) virt, fd, offset, len, r, w, x);
312 printk("try increasing the host's "
313 "/proc/sys/vm/max_map_count to <physical "
314 "memory size>/4096\n");
315 panic("map_memory(0x%lx, %d, 0x%llx, %ld, %d, %d, %d) failed, "
316 "err = %d\n", virt, fd, offset, len, r, w, x, err);
320 extern int __syscall_stub_start, __binary_start;
322 void setup_physmem(unsigned long start, unsigned long reserve_end,
323 unsigned long len, unsigned long long highmem)
325 unsigned long reserve = reserve_end - start;
326 int pfn = PFN_UP(__pa(reserve_end));
327 int delta = (len - reserve) >> PAGE_SHIFT;
328 int err, offset, bootmap_size;
330 physmem_fd = create_mem_file(len + highmem);
332 offset = uml_reserved - uml_physmem;
333 err = os_map_memory((void *) uml_reserved, physmem_fd, offset,
334 len - offset, 1, 1, 0);
336 os_print_error(err, "Mapping memory");
340 /* Special kludge - This page will be mapped in to userspace processes
341 * from physmem_fd, so it needs to be written out there.
343 os_seek_file(physmem_fd, __pa(&__syscall_stub_start));
344 os_write_file(physmem_fd, &__syscall_stub_start, PAGE_SIZE);
346 bootmap_size = init_bootmem(pfn, pfn + delta);
347 free_bootmem(__pa(reserve_end) + bootmap_size,
348 len - bootmap_size - reserve);
351 int phys_mapping(unsigned long phys, __u64 *offset_out)
353 struct phys_desc *desc = find_phys_mapping(__va(phys & PAGE_MASK));
358 *offset_out = desc->offset;
360 else if(phys < physmem_size){
364 else if(phys < __pa(end_iomem)){
365 struct iomem_region *region = iomem_regions;
367 while(region != NULL){
368 if((phys >= region->phys) &&
369 (phys < region->phys + region->size)){
371 *offset_out = phys - region->phys;
374 region = region->next;
377 else if(phys < __pa(end_iomem) + highmem){
379 *offset_out = phys - iomem_size;
385 static int __init uml_mem_setup(char *line, int *add)
388 physmem_size = memparse(line,&retptr);
391 __uml_setup("mem=", uml_mem_setup,
392 "mem=<Amount of desired ram>\n"
393 " This controls how much \"physical\" memory the kernel allocates\n"
394 " for the system. The size is specified as a number followed by\n"
395 " one of 'k', 'K', 'm', 'M', which have the obvious meanings.\n"
396 " This is not related to the amount of memory in the host. It can\n"
397 " be more, and the excess, if it's ever used, will just be swapped out.\n"
398 " Example: mem=64M\n\n"
401 unsigned long find_iomem(char *driver, unsigned long *len_out)
403 struct iomem_region *region = iomem_regions;
405 while(region != NULL){
406 if(!strcmp(region->driver, driver)){
407 *len_out = region->size;
408 return(region->virt);
411 region = region->next;
417 int setup_iomem(void)
419 struct iomem_region *region = iomem_regions;
420 unsigned long iomem_start = high_physmem + PAGE_SIZE;
423 while(region != NULL){
424 err = os_map_memory((void *) iomem_start, region->fd, 0,
425 region->size, 1, 1, 0);
427 printk("Mapping iomem region for driver '%s' failed, "
428 "errno = %d\n", region->driver, -err);
430 region->virt = iomem_start;
431 region->phys = __pa(region->virt);
434 iomem_start += region->size + PAGE_SIZE;
435 region = region->next;
441 __initcall(setup_iomem);
444 * Overrides for Emacs so that we follow Linus's tabbing style.
445 * Emacs will notice this stuff at the end of the file and automatically
446 * adjust the settings for this buffer only. This must remain at the end
448 * ---------------------------------------------------------------------------
450 * c-file-style: "linux"