2 * arch/um/kernel/mem_user.c
4 * BRIEF MODULE DESCRIPTION
5 * user side memory routines for supporting IO memory inside user mode linux
7 * Copyright (C) 2001 RidgeRun, Inc.
8 * Author: RidgeRun, Inc.
9 * Greg Lonnon glonnon@ridgerun.com or info@ridgerun.com
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
22 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 675 Mass Ave, Cambridge, MA 02139, USA.
40 #include <sys/types.h>
42 #include "kern_util.h"
44 #include "user_util.h"
49 #include "kern_constants.h"
51 extern struct mem_region physmem_region;
53 #define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
55 static int create_tmp_file(unsigned long len)
60 fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
62 os_print_error(fd, "make_tempfile");
66 err = os_mode_fd(fd, 0777);
68 os_print_error(err, "os_mode_fd");
71 err = os_seek_file(fd, len);
73 os_print_error(err, "os_seek_file");
77 err = os_write_file(fd, &zero, 1);
79 os_print_error(err, "os_write_file");
86 static int have_devanon = 0;
88 void check_devanon(void)
92 printk("Checking for /dev/anon on the host...");
93 fd = open("/dev/anon", O_RDWR);
95 printk("Not available (open failed with errno %d)\n", errno);
103 static int create_anon_file(unsigned long len)
108 fd = open("/dev/anon", O_RDWR);
110 os_print_error(fd, "opening /dev/anon");
114 addr = mmap(NULL, len, PROT_READ | PROT_WRITE , MAP_PRIVATE, fd, 0);
115 if(addr == MAP_FAILED){
116 os_print_error((int) addr, "mapping physmem file");
124 int create_mem_file(unsigned long len)
129 fd = create_anon_file(len);
130 else fd = create_tmp_file(len);
132 err = os_set_exec_close(fd, 1);
134 os_print_error(err, "exec_close");
138 struct iomem_region *iomem_regions = NULL;
141 static int __init parse_iomem(char *str, int *add)
143 struct iomem_region *new;
149 file = strchr(str,',');
151 printf("parse_iomem : failed to parse iomem\n");
156 fd = os_open_file(file, of_rdwr(OPENFLAGS()), 0);
158 os_print_error(fd, "parse_iomem - Couldn't open io file");
162 err = os_stat_fd(fd, &buf);
164 os_print_error(err, "parse_iomem - cannot stat_fd file");
168 new = malloc(sizeof(*new));
170 perror("Couldn't allocate iomem_region struct");
174 size = (buf.ust_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
176 *new = ((struct iomem_region) { .next = iomem_regions,
183 iomem_size += new->size + UM_KERN_PAGE_SIZE;
192 __uml_setup("iomem=", parse_iomem,
193 "iomem=<name>,<file>\n"
194 " Configure <file> as an IO memory region named <name>.\n\n"
197 int protect_memory(unsigned long addr, unsigned long len, int r, int w, int x,
202 err = os_protect_memory((void *) addr, len, r, w, x);
205 panic("protect failed, err = %d", -err);
212 /* Debugging facility for dumping stuff out to the host, avoiding the timing
213 * problems that come with printf and breakpoints.
214 * Enable in case of emergency.
220 int logging_line = 0;
221 char logging_buf[512];
223 void log(char *fmt, ...)
227 struct openflags flags;
229 if(logging == 0) return;
231 flags = of_create(of_trunc(of_rdwr(OPENFLAGS())));
232 logging_fd = os_open_file("log", flags, 0644);
234 gettimeofday(&tv, NULL);
235 sprintf(logging_buf, "%d\t %u.%u ", logging_line++, tv.tv_sec,
238 vsprintf(&logging_buf[strlen(logging_buf)], fmt, ap);
240 write(logging_fd, logging_buf, strlen(logging_buf));
245 * Overrides for Emacs so that we follow Linus's tabbing style.
246 * Emacs will notice this stuff at the end of the file and automatically
247 * adjust the settings for this buffer only. This must remain at the end
249 * ---------------------------------------------------------------------------
251 * c-file-style: "linux"