vserver 1.9.3
[linux-2.6.git] / arch / um / kernel / mem_user.c
1 /*
2  * arch/um/kernel/mem_user.c
3  *
4  * BRIEF MODULE DESCRIPTION
5  * user side memory routines for supporting IO memory inside user mode linux
6  *
7  * Copyright (C) 2001 RidgeRun, Inc.
8  * Author: RidgeRun, Inc.
9  *         Greg Lonnon glonnon@ridgerun.com or info@ridgerun.com
10  *
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.
15  *
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.
26  *
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.
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <stddef.h>
35 #include <stdarg.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <fcntl.h>
40 #include <sys/types.h>
41 #include <sys/mman.h>
42 #include "kern_util.h"
43 #include "user.h"
44 #include "user_util.h"
45 #include "mem_user.h"
46 #include "init.h"
47 #include "os.h"
48 #include "tempfile.h"
49 #include "kern_constants.h"
50
51 extern struct mem_region physmem_region;
52
53 #define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
54
55 static int create_tmp_file(unsigned long len)
56 {
57         int fd, err;
58         char zero;
59
60         fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
61         if(fd < 0) {
62                 os_print_error(fd, "make_tempfile");
63                 exit(1);
64         }
65
66         err = os_mode_fd(fd, 0777);
67         if(err < 0){
68                 os_print_error(err, "os_mode_fd");
69                 exit(1);
70         }
71         err = os_seek_file(fd, len);
72         if(err < 0){
73                 os_print_error(err, "os_seek_file");
74                 exit(1);
75         }
76         zero = 0;
77         err = os_write_file(fd, &zero, 1);
78         if(err != 1){
79                 os_print_error(err, "os_write_file");
80                 exit(1);
81         }
82
83         return(fd);
84 }
85
86 static int have_devanon = 0;
87
88 void check_devanon(void)
89 {
90         int fd;
91
92         printk("Checking for /dev/anon on the host...");
93         fd = open("/dev/anon", O_RDWR);
94         if(fd < 0){
95                 printk("Not available (open failed with errno %d)\n", errno);
96                 return;
97         }
98
99         printk("OK\n");
100         have_devanon = 1;
101 }
102
103 static int create_anon_file(unsigned long len)
104 {
105         void *addr;
106         int fd;
107
108         fd = open("/dev/anon", O_RDWR);
109         if(fd < 0) {
110                 os_print_error(fd, "opening /dev/anon");
111                 exit(1);
112         }
113
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");
117                 exit(1);
118         }
119         munmap(addr, len);
120
121         return(fd);
122 }
123
124 int create_mem_file(unsigned long len)
125 {
126         int err, fd;
127
128         if(have_devanon)
129                 fd = create_anon_file(len);
130         else fd = create_tmp_file(len);
131
132         err = os_set_exec_close(fd, 1);
133         if(err < 0)
134                 os_print_error(err, "exec_close");
135         return(fd);
136 }
137
138 struct iomem_region *iomem_regions = NULL;
139 int iomem_size = 0;
140
141 static int __init parse_iomem(char *str, int *add)
142 {
143         struct iomem_region *new;
144         struct uml_stat buf;
145         char *file, *driver;
146         int fd, err, size;
147
148         driver = str;
149         file = strchr(str,',');
150         if(file == NULL){
151                 printf("parse_iomem : failed to parse iomem\n");
152                 goto out;
153         }
154         *file = '\0';
155         file++;
156         fd = os_open_file(file, of_rdwr(OPENFLAGS()), 0);
157         if(fd < 0){
158                 os_print_error(fd, "parse_iomem - Couldn't open io file");
159                 goto out;
160         }
161
162         err = os_stat_fd(fd, &buf);
163         if(err < 0){
164                 os_print_error(err, "parse_iomem - cannot stat_fd file");
165                 goto out_close;
166         }
167
168         new = malloc(sizeof(*new));
169         if(new == NULL){
170                 perror("Couldn't allocate iomem_region struct");
171                 goto out_close;
172         }
173
174         size = (buf.ust_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
175
176         *new = ((struct iomem_region) { .next           = iomem_regions,
177                                         .driver         = driver,
178                                         .fd             = fd,
179                                         .size           = size,
180                                         .phys           = 0,
181                                         .virt           = 0 });
182         iomem_regions = new;
183         iomem_size += new->size + UM_KERN_PAGE_SIZE;
184
185         return(0);
186  out_close:
187         os_close_file(fd);
188  out:
189         return(1);
190 }
191
192 __uml_setup("iomem=", parse_iomem,
193 "iomem=<name>,<file>\n"
194 "    Configure <file> as an IO memory region named <name>.\n\n"
195 );
196
197 int protect_memory(unsigned long addr, unsigned long len, int r, int w, int x,
198                    int must_succeed)
199 {
200         int err;
201
202         err = os_protect_memory((void *) addr, len, r, w, x);
203         if(err < 0){
204                 if(must_succeed)
205                         panic("protect failed, err = %d", -err);
206                 else return(err);
207         }
208         return(0);
209 }
210
211 /*
212  * Overrides for Emacs so that we follow Linus's tabbing style.
213  * Emacs will notice this stuff at the end of the file and automatically
214  * adjust the settings for this buffer only.  This must remain at the end
215  * of the file.
216  * ---------------------------------------------------------------------------
217  * Local variables:
218  * c-file-style: "linux"
219  * End:
220  */