UbixOS V2  2.0
vmm_mmap.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2002-2018 The UbixOS Project.
3  * All rights reserved.
4  *
5  * This was developed by Christopher W. Olsen for the UbixOS Project.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are permitted
8  * provided that the following conditions are met:
9  *
10  * 1) Redistributions of source code must retain the above copyright notice, this list of
11  * conditions, the following disclaimer and the list of authors.
12  * 2) Redistributions in binary form must reproduce the above copyright notice, this list of
13  * conditions, the following disclaimer and the list of authors in the documentation and/or
14  * other materials provided with the distribution.
15  * 3) Neither the name of the UbixOS Project nor the names of its contributors may be used to
16  * endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <vmm/vmm.h>
30 #include <sys/types.h>
31 #include <lib/kprintf.h>
32 #include <sys/descrip.h>
33 #include <ubixos/kpanic.h>
34 #include <ubixos/spinlock.h>
35 #include <ubixos/sched.h>
36 
37 /* MrOlsen (2016-01-15) TEMP: Put These somewhere else */
40 #define EINVAL 22 /* Invalid argument */
41 #define MAP_ALIGNED(n) ((n) << MAP_ALIGNMENT_SHIFT)
42 #define MAP_ALIGNMENT_SHIFT 24
43 #define MAP_ALIGNMENT_MASK MAP_ALIGNED(0xff)
44 #define MAP_ALIGNED_SUPER MAP_ALIGNED(1) /* align on a superpage */
45 #define NBBY 8 /* number of bits in a byte */
46 
47 /* PROTs */
48 #define PROT_NONE 0x00 /* no permissions */
49 #define PROT_READ 0x01 /* pages can be read */
50 #define PROT_WRITE 0x02 /* pages can be written */
51 #define PROT_EXEC 0x04 /* pages can be executed */
52 
53 /* FLAGs */
54 
55 /*
56  * Do I Need These?
57  */
58 #define MAP_SHARED 0x0001 /* share changes */
59 #define MAP_PRIVATE 0x0002 /* changes are private */
60 #define MAP_FIXED 0x0010 /* map addr must be exactly as requested */
61 #define MAP_RENAME 0x0020 /* Sun: rename private pages to file */
62 #define MAP_NORESERVE 0x0040 /* Sun: don't reserve needed swap area */
63 #define MAP_RESERVED0080 0x0080 /* previously misimplemented MAP_INHERIT */
64 #define MAP_RESERVED0100 0x0100 /* previously unimplemented MAP_NOEXTEND */
65 #define MAP_HASSEMAPHORE 0x0200 /* region may contain semaphores */
66 #define MAP_STACK 0x0400 /* region grows down, like a stack */
67 #define MAP_NOSYNC 0x0800 /* page to but do not sync underlying file */
68 
69 /*
70  * Mapping type
71  */
72 #define MAP_FILE 0x0000 /* map from file (default) */
73 #define MAP_ANON 0x1000 /* allocated from memory, swap space */
74 
75 int freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap) {
76  vm_size_t size, pageoff;
77  off_t pos;
78  int align, flags, error;
79  vm_offset_t addr;
80 
81  int i;
82 
83  error = 0;
84 
85  size = uap->len;
86  flags = uap->flags;
87  pos = uap->pos;
88 
89  kprintf("uap->flags: [0x%X]\n", uap->flags);
90  kprintf("uap->addr: [0x%X]\n", uap->addr);
91  kprintf("uap->len: [0x%X]\n", uap->len);
92  kprintf("uap->prot: [0x%X]\n", uap->prot);
93  kprintf("uap->fd: [%i]\n", uap->fd);
94  kprintf("uap->pad: [0x%X]\n", uap->pad);
95  kprintf("uap->pos: [0x%X]\n", uap->pos);
96 
97  if ((uap->flags & MAP_ANON) != 0)
98  pos = 0;
99 
100  /*
101  * Align the file position to a page boundary,
102  * and save its page offset component.
103  */
104 
105  pageoff = (pos & PAGE_MASK);
106  pos -= pageoff;
107 
108  /* Adjust size for rounding (on both ends). */
109  size += pageoff;
110  size = (vm_size_t) round_page(size);
111 
112  /* Ensure alignment is at least a page and fits in a pointer. */
113  align = flags & MAP_ALIGNMENT_MASK;
114  if (align != 0 && align != MAP_ALIGNED_SUPER && (align >> MAP_ALIGNMENT_SHIFT >= sizeof(void *) * NBBY || align >> MAP_ALIGNMENT_SHIFT < PAGE_SHIFT))
115  return (EINVAL);
116 
117  if (flags & MAP_FIXED) {
118  kprintf("FIXED NOT SUPPORTED YET");
119  return (EINVAL);
120  }
121  else {
122  /* At Some Point I Need To Proc Lock Incase It's Threaded */
123  /* MrOlsen (2016-01-15) Temporary comment out
124  if ( addr == 0 || (addr >= round_page( (vm_offset_t) vms->vm_taddr ) && addr < round_page( (vm_offset_t) vms->vm_daddr + lim_max( td->td_proc, RLIMIT_DATA ) )) )
125  addr = round_page( (vm_offset_t) vms->vm_daddr + lim_max( td->td_proc, RLIMIT_DATA ) );
126  */
127  }
128 
129  if (flags & MAP_ANON) {
130  /*
131  * Mapping blank space is trivial.
132  */
133 
134  /*
135  handle = NULL;
136  handle_type = OBJT_DEFAULT;
137  maxprot = VM_PROT_ALL;
138  cap_maxprot = VM_PROT_ALL;
139  */
140  for (i = addr; i < (addr + size); i += 0x1000) {
142  K_PANIC("remap Failed");
143  }
144  kprintf("td->vm_dsize should be adjust but isn't");
145  }
146  else {
147  /* Mapping File */
148  kprintf("File Mapping Not Supported Yet");
149  return (EINVAL);
150  }
151 
152  return (0x0);
153 }
154 
155 int sys_munmap(struct thread *td, struct sys_munmap_args *uap) {
156  //TEMP
157  td->td_retval[0] = 0;
158  return (0);
159 }
160 ;
161 
162 int sys_mmap(struct thread *td, struct sys_mmap_args *uap) {
163  vm_offset_t addr = 0x0;
164  char *tmp = 0x0;
165  struct file *fd = 0x0;
166  int x;
167 
168  addr = (vm_offset_t) uap->addr;
169 
170  if (uap->fd == -1) {
171  if (uap->addr != 0x0) {
172  for (x = 0x0; x < round_page(uap->len); x += 0x1000) {
173  vmm_unmapPage(((uint32_t) uap->addr & 0xFFFFF000) + x, VMM_FREE);
174  /* Make readonly and read/write !!! */
175  if (vmm_remapPage(vmm_findFreePage(_current->id), (((uint32_t) uap->addr & 0xFFFFF000) + x), PAGE_DEFAULT, _current->id, 0) == 0x0)
176  K_PANIC("Remap Page Failed");
177 
178  }
179  tmp = uap->addr;
180  bzero(tmp, uap->len);
181  td->td_retval[0] = (uint32_t) tmp;
182  return (0x0);
183  }
184 
185  td->td_retval[0] = vmm_getFreeVirtualPage(_current->id, round_page( uap->len ) / 0x1000, VM_TASK);
186  bzero(td->td_retval[0], uap->len);
187  return (0x0); //vmm_getFreeVirtualPage(_current->id, round_page( uap->len ) / 0x1000, VM_THRD));
188  }
189  else {
190 
191  getfd(td, &fd, uap->fd);
192 
193  if (uap->addr == 0x0)
194  tmp = (char *) vmm_getFreeVirtualPage(_current->id, round_page(uap->len) / 0x1000, VM_TASK);
195  else {
196 
197  for (x = 0x0; x < round_page(uap->len); x += 0x1000) {
198 
199  vmm_unmapPage(((uint32_t) uap->addr & 0xFFFFF000) + x, 1);
200 
201  /* Make readonly and read/write !!! */
202  if (vmm_remapPage(vmm_findFreePage(_current->id), (((uint32_t) uap->addr & 0xFFFFF000) + x), PAGE_DEFAULT, _current->id, 0) == 0x0)
203  K_PANIC("Remap Page Failed");
204 
205  }
206 
207  tmp = uap->addr;
208 
209  }
210 
211  fseek(fd->fd, uap->pos, 0x0);
212  fread(tmp, uap->len, 0x1, fd->fd);
213 
214  td->td_retval[0] = (uint32_t) tmp;
215 
216  if (td->td_retval[0] == (caddr_t) -1)
217  kpanic("MMAP_FAILED");
218  }
219  return (0x0);
220 }
freebsd6_mmap_args::len
size_t len
Definition: vmm.h:79
spinlock.h
MAP_ANON
#define MAP_ANON
Definition: vmm_mmap.c:73
MAP_ALIGNMENT_SHIFT
#define MAP_ALIGNMENT_SHIFT
Definition: vmm_mmap.c:42
sys_munmap
int sys_munmap(struct thread *td, struct sys_munmap_args *uap)
Definition: vmm_mmap.c:155
freebsd6_mmap_args::addr
caddr_t addr
Definition: vmm.h:75
K_PANIC
#define K_PANIC(msg)
Definition: kpanic.h:32
__uint32_t
unsigned int __uint32_t
Definition: _types.h:38
sys_mmap_args::addr
caddr_t addr
Definition: sysproto_posix.h:380
freebsd6_mmap_args::pos
off_t pos
Definition: vmm.h:98
sys_mmap
int sys_mmap(struct thread *td, struct sys_mmap_args *uap)
Definition: vmm_mmap.c:162
NBBY
#define NBBY
Definition: vmm_mmap.c:45
freebsd6_mmap_args::fd
int fd
Definition: vmm.h:90
fread
size_t fread(void *ptr, size_t size, size_t nmemb, fileDescriptor_t *fd)
Definition: file.c:291
file
Definition: descrip.h:67
thread
Definition: thread.h:40
vm_offset_t
__uint32_t vm_offset_t
Definition: types.h:120
vmm.h
round_page
#define round_page(x)
Definition: paging.h:72
sched.h
kpanic
void kpanic(const char *fmt,...)
print panic message and halt system
Definition: kpanic.c:41
types.h
bzero
#define bzero(buf, size)
Definition: gpt.h:37
vmm_unmapPage
void vmm_unmapPage(uint32_t, unmapFlags_t)
Definition: unmappage.c:47
VM_TASK
#define VM_TASK
Definition: paging.h:52
kpanic.h
taskStruct::id
pidType id
Definition: sched.h:63
vmm_findFreePage
uint32_t vmm_findFreePage(pidType pid)
Definition: vmm_memory.c:221
vmm_getFreeVirtualPage
void * vmm_getFreeVirtualPage(pidType, int, int)
Definition: getfreevirtualpage.c:47
file::fd
fileDescriptor_t * fd
Definition: descrip.h:71
thread::td_retval
int td_retval[2]
Definition: thread.h:41
kprintf.h
EINVAL
#define EINVAL
Definition: vmm_mmap.c:40
PAGE_DEFAULT
#define PAGE_DEFAULT
Definition: paging.h:68
getfd
int getfd(struct thread *td, struct file **fp, int fd)
get pointer to file fd in specified thread
Definition: descrip.c:214
sys_mmap_args::len
size_t len
Definition: sysproto_posix.h:383
vmm_remapPage
int vmm_remapPage(uint32_t, uint32_t, uint16_t, pidType, int haveLock)
Definition: paging.c:199
uint32_t
__uint32_t uint32_t
Definition: types.h:46
sys_mmap_args::pos
off_t pos
Definition: sysproto_posix.h:395
sys_mmap_args::fd
int fd
Definition: sysproto_posix.h:392
freebsd6_mmap_args
Definition: vmm.h:73
caddr_t
char * caddr_t
Definition: types.h:41
_current
kTask_t * _current
Definition: sched.c:50
MAP_FIXED
#define MAP_FIXED
Definition: vmm_mmap.c:60
freebsd6_mmap
int freebsd6_mmap(struct thread *td, struct freebsd6_mmap_args *uap)
Definition: vmm_mmap.c:75
descrip.h
VMM_FREE
Definition: vmm.h:111
sys_mmap_args
Definition: sysproto_posix.h:378
freebsd6_mmap_args::flags
int flags
Definition: vmm.h:87
PAGE_SHIFT
#define PAGE_SHIFT
Definition: paging.h:36
off_t
__int64_t off_t
Definition: types.h:119
kprintf
int kprintf(const char *,...)
Definition: kprintf.c:259
PAGE_MASK
#define PAGE_MASK
Definition: paging.h:38
MAP_ALIGNMENT_MASK
#define MAP_ALIGNMENT_MASK
Definition: vmm_mmap.c:43
MAP_ALIGNED_SUPER
#define MAP_ALIGNED_SUPER
Definition: vmm_mmap.c:44
__vm_size_t
__uint32_t __vm_size_t
Definition: vmm_mmap.c:38
freebsd6_mmap_args::prot
int prot
Definition: vmm.h:83
vm_size_t
__vm_size_t vm_size_t
Definition: vmm_mmap.c:39
freebsd6_mmap_args::pad
int pad
Definition: vmm.h:94
sys_munmap_args
Definition: sysproto_posix.h:439
fseek
int fseek(fileDescriptor_t *tmpFd, long offset, int whence)
Definition: file.c:326