Fix memory leak in discard case of sctp_sf_abort_violation()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / um / kernel / physmem.c
blobe66432f42485dad02584bb13a6bc28a626aa0814
1 /*
2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
6 #include "linux/bootmem.h"
7 #include "linux/mm.h"
8 #include "linux/pfn.h"
9 #include "asm/page.h"
10 #include "as-layout.h"
11 #include "init.h"
12 #include "kern.h"
13 #include "mem_user.h"
14 #include "os.h"
16 static int physmem_fd = -1;
18 /* Changed during early boot */
19 unsigned long high_physmem;
21 extern unsigned long long physmem_size;
23 int __init init_maps(unsigned long physmem, unsigned long iomem,
24 unsigned long highmem)
26 struct page *p, *map;
27 unsigned long phys_len, phys_pages, highmem_len, highmem_pages;
28 unsigned long iomem_len, iomem_pages, total_len, total_pages;
29 int i;
31 phys_pages = physmem >> PAGE_SHIFT;
32 phys_len = phys_pages * sizeof(struct page);
34 iomem_pages = iomem >> PAGE_SHIFT;
35 iomem_len = iomem_pages * sizeof(struct page);
37 highmem_pages = highmem >> PAGE_SHIFT;
38 highmem_len = highmem_pages * sizeof(struct page);
40 total_pages = phys_pages + iomem_pages + highmem_pages;
41 total_len = phys_len + iomem_len + highmem_len;
43 map = alloc_bootmem_low_pages(total_len);
44 if (map == NULL)
45 return -ENOMEM;
47 for (i = 0; i < total_pages; i++) {
48 p = &map[i];
49 memset(p, 0, sizeof(struct page));
50 SetPageReserved(p);
51 INIT_LIST_HEAD(&p->lru);
54 max_mapnr = total_pages;
55 return 0;
58 /* Changed during early boot */
59 static unsigned long kmem_top = 0;
61 unsigned long get_kmem_end(void)
63 if (kmem_top == 0)
64 kmem_top = host_task_size - 1024 * 1024;
65 return kmem_top;
68 void map_memory(unsigned long virt, unsigned long phys, unsigned long len,
69 int r, int w, int x)
71 __u64 offset;
72 int fd, err;
74 fd = phys_mapping(phys, &offset);
75 err = os_map_memory((void *) virt, fd, offset, len, r, w, x);
76 if (err) {
77 if (err == -ENOMEM)
78 printk(KERN_ERR "try increasing the host's "
79 "/proc/sys/vm/max_map_count to <physical "
80 "memory size>/4096\n");
81 panic("map_memory(0x%lx, %d, 0x%llx, %ld, %d, %d, %d) failed, "
82 "err = %d\n", virt, fd, offset, len, r, w, x, err);
86 extern int __syscall_stub_start;
88 void __init setup_physmem(unsigned long start, unsigned long reserve_end,
89 unsigned long len, unsigned long long highmem)
91 unsigned long reserve = reserve_end - start;
92 int pfn = PFN_UP(__pa(reserve_end));
93 int delta = (len - reserve) >> PAGE_SHIFT;
94 int err, offset, bootmap_size;
96 physmem_fd = create_mem_file(len + highmem);
98 offset = uml_reserved - uml_physmem;
99 err = os_map_memory((void *) uml_reserved, physmem_fd, offset,
100 len - offset, 1, 1, 1);
101 if (err < 0) {
102 printf("setup_physmem - mapping %ld bytes of memory at 0x%p "
103 "failed - errno = %d\n", len - offset,
104 (void *) uml_reserved, err);
105 exit(1);
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);
115 bootmap_size = init_bootmem(pfn, pfn + delta);
116 free_bootmem(__pa(reserve_end) + bootmap_size,
117 len - bootmap_size - reserve);
120 int phys_mapping(unsigned long phys, unsigned long long *offset_out)
122 int fd = -1;
124 if (phys < physmem_size) {
125 fd = physmem_fd;
126 *offset_out = phys;
128 else if (phys < __pa(end_iomem)) {
129 struct iomem_region *region = iomem_regions;
131 while (region != NULL) {
132 if ((phys >= region->phys) &&
133 (phys < region->phys + region->size)) {
134 fd = region->fd;
135 *offset_out = phys - region->phys;
136 break;
138 region = region->next;
141 else if (phys < __pa(end_iomem) + highmem) {
142 fd = physmem_fd;
143 *offset_out = phys - iomem_size;
146 return fd;
149 static int __init uml_mem_setup(char *line, int *add)
151 char *retptr;
152 physmem_size = memparse(line,&retptr);
153 return 0;
155 __uml_setup("mem=", uml_mem_setup,
156 "mem=<Amount of desired ram>\n"
157 " This controls how much \"physical\" memory the kernel allocates\n"
158 " for the system. The size is specified as a number followed by\n"
159 " one of 'k', 'K', 'm', 'M', which have the obvious meanings.\n"
160 " This is not related to the amount of memory in the host. It can\n"
161 " be more, and the excess, if it's ever used, will just be swapped out.\n"
162 " Example: mem=64M\n\n"
165 extern int __init parse_iomem(char *str, int *add);
167 __uml_setup("iomem=", parse_iomem,
168 "iomem=<name>,<file>\n"
169 " Configure <file> as an IO memory region named <name>.\n\n"
173 * This list is constructed in parse_iomem and addresses filled in in
174 * setup_iomem, both of which run during early boot. Afterwards, it's
175 * unchanged.
177 struct iomem_region *iomem_regions = NULL;
179 /* Initialized in parse_iomem */
180 int iomem_size = 0;
182 unsigned long find_iomem(char *driver, unsigned long *len_out)
184 struct iomem_region *region = iomem_regions;
186 while (region != NULL) {
187 if (!strcmp(region->driver, driver)) {
188 *len_out = region->size;
189 return region->virt;
192 region = region->next;
195 return 0;
198 int setup_iomem(void)
200 struct iomem_region *region = iomem_regions;
201 unsigned long iomem_start = high_physmem + PAGE_SIZE;
202 int err;
204 while (region != NULL) {
205 err = os_map_memory((void *) iomem_start, region->fd, 0,
206 region->size, 1, 1, 0);
207 if (err)
208 printk(KERN_ERR "Mapping iomem region for driver '%s' "
209 "failed, errno = %d\n", region->driver, -err);
210 else {
211 region->virt = iomem_start;
212 region->phys = __pa(region->virt);
215 iomem_start += region->size + PAGE_SIZE;
216 region = region->next;
219 return 0;
222 __initcall(setup_iomem);