aboutsummaryrefslogtreecommitdiff
path: root/kernel/vm/mmap.c
blob: a298df4abcab14213eee21f9985f5e20d40b1685 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "vm/mmap.h"
#include "errno.h"
#include "fs/file.h"
#include "fs/vfs.h"
#include "fs/vnode.h"
#include "globals.h"
#include "mm/mm.h"
#include "mm/mman.h"
#include "mm/tlb.h"
#include "util/debug.h"

/*
 * This function implements the mmap(2) syscall: Add a mapping to the current
 * process's address space. Supports the following flags: MAP_SHARED,
 * MAP_PRIVATE, MAP_FIXED, and MAP_ANON.
 *
 *  ret - If provided, on success, *ret must point to the start of the mapped area
 *
 * Return 0 on success, or:
 *  - EACCES: 
 *     - a file mapping was requested, but fd is not open for reading. 
 *     - MAP_SHARED was requested and PROT_WRITE is set, but fd is
 *       not open in read/write (O_RDWR) mode.
 *     - PROT_WRITE is set, but the file has FMODE_APPEND specified.
 *  - EBADF:
 *     - fd is not a valid file descriptor and MAP_ANON was
 *       not set
 *  - EINVAL:
 *     - addr is not page aligned and MAP_FIXED is specified 
 *     - addr is out of range of the user address space and MAP_FIXED is specified
 *     - off is not page aligned
 *     - len is <= 0 or off < 0
 *     - flags do not contain MAP_PRIVATE or MAP_SHARED
 *  - ENODEV:
 *     - The underlying filesystem of the specified file does not
 *       support memory mapping or in other words, the file's vnode's mmap
 *       operation doesn't exist
 *  - Propagate errors from vmmap_map()
 * 
 *  See the errors section of the mmap(2) man page for more details
 * 
 * Hints:
 *  1) A lot of error checking.
 *  2) Call vmmap_map() to create the mapping.
 *     a) Use VMMAP_DIR_HILO as default, which will make other stencil code in
 *        Weenix happy.
 *  3) Call tlb_flush_range() on the newly-mapped region. This is because the
 *     newly-mapped region could have been used by someone else, and you don't
 *     want to get stale mappings.
 *  4) Don't forget to set ret if it was provided.
 * 
 *  If you are mapping less than a page, make sure that you are still allocating 
 *  a full page.
 */
long do_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off,
             void **ret)
{
    // NOT_YET_IMPLEMENTED("VM: do_mmap");

    // check if addr is page aligned when MAP_FIXED is specified
    if (PAGE_ALIGNED(addr) == 0 && (flags & MAP_FIXED))
    {
        return -EINVAL;
    }

    // check if MAP_FIXED is specified and addr is out of range of the user address space
    if ((flags & MAP_FIXED) && ((uintptr_t)addr < USER_MEM_LOW || (uintptr_t)addr + len > USER_MEM_HIGH))
    {
        return -EINVAL;
    }

    // check if len is not zero (len is an unsigned value, so it is always positive)
    if ((ssize_t) len <= 0)
    {
        return -EINVAL;
    }

    // check if offset is positive and aligned
    if (off < 0 || PAGE_ALIGNED(off) == 0)
    {
        return -EINVAL;
    }

    // check if flags do not contain MAP_PRIVATE or MAP_SHARED
    if ((flags & MAP_PRIVATE) == 0 && (flags & MAP_SHARED) == 0)
    {
        return -EINVAL;
    }

    // check if the fd is valid and MAP_ANON was not set
    if (((fd < 0 || fd >= NFILES) || curproc->p_files[fd] == NULL) && !(flags & MAP_ANON))
    {
        return -EBADF;
    }

    // check if a file mapping was requested, but fd is not open for reading
    // file error checking is done in if statement below
    file_t *file = NULL;
    if (fd >= 0 && fd < NFILES)
    {
        // get the file and check if it is valid
        file = curproc->p_files[fd];
        if (file == NULL)
        {
            return -EBADF;
        }

        // ENODEV CHECKS

        // check if the file's vnode's mmap operation doesn't exist
        if (file->f_vnode->vn_ops == NULL || file->f_vnode->vn_ops->mmap == NULL)
        {
            return -ENODEV;
        }

        // ACCESS CHECKS

        // check if thef FMODE_READ flag is not set
        if ((file->f_mode & FMODE_READ) == 0)
        {
            return -EACCES;
        }
        
        // check if append mode is set and PROT_WRITE is set
        if ((prot & PROT_WRITE) && (file->f_mode & FMODE_APPEND))
        {
            return -EACCES;
        }

        // if MAP_SHARED was requested and PROT_WRITE is set, but fd is not open in read/write (O_RDWR) mode.
        if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && (file->f_mode & FMODE_WRITE) == 0)
        {
            return -EACCES;
        }

        // check if PROT_WRITE is set, but the file has FMODE_APPEND specified
        if ((prot & PROT_WRITE) && (file->f_mode & FMODE_APPEND))
        {
            return -EACCES;
        }
    }


    // Now that error checking is done, we can proceed with the mapping
    vmarea_t *vma = NULL;
    long err = vmmap_map(
        curproc->p_vmmap, 
        file ? file->f_vnode : NULL,
        ADDR_TO_PN(PAGE_ALIGN_DOWN(addr)),
        ADDR_TO_PN(PAGE_ALIGN_UP((uintptr_t)addr + len)) - ADDR_TO_PN(PAGE_ALIGN_DOWN(addr)), 
        prot, 
        flags, 
        off, 
        VMMAP_DIR_HILO,
        &vma
    );
    // check if vmmap_map() failed
    if (err < 0)
    {
        return err;
    }

    void *start = PN_TO_ADDR(vma->vma_start);
    // flush the TLB
    tlb_flush_range(
        (uintptr_t) start,
        PAGE_SIZE * (vma->vma_end - vma->vma_start)
    );

    // set ret if it was provided and return 0 on success
    if (ret)
    {
        *ret = start;
    }
    return 0;
}

/*
 * This function implements the munmap(2) syscall.
 *
 * Return 0 on success, or:
 *  - EINVAL:
 *     - addr is not aligned on a page boundary
 *     - the region to unmap is out of range of the user address space
 *     - len is 0
 *  - Propagate errors from vmmap_remove()
 * 
 *  See the errors section of the munmap(2) man page for more details
 *
 * Hints:
 *  - Similar to do_mmap():
 *  1) Perform error checking.
 *  2) Call vmmap_remove().
 */
long do_munmap(void *addr, size_t len)
{
    // NOT_YET_IMPLEMENTED("VM: do_munmap");

    // Check if addr is page aligned
    if (PAGE_ALIGNED(addr) == 0)
    {
        return -EINVAL;
    }

    // Check if len is in bounds
    if (len > USER_MEM_HIGH || len <= 0)
    {
        return -EINVAL;
    }

    // Check if the addr is out of range of the user address space
    if (
        (uintptr_t)addr < USER_MEM_LOW  
        || (uintptr_t)addr > USER_MEM_HIGH
        || (uintptr_t)addr + len > USER_MEM_HIGH
        )
    {
        return -EINVAL;
    }

    // Remove the mapping
    size_t start = ADDR_TO_PN(addr);
    size_t end = ADDR_TO_PN(PAGE_ALIGN_UP((uintptr_t)addr + len));
    long ret = vmmap_remove(
        curproc->p_vmmap,
        start,
        end - start);
    return ret;
}