OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [mm/] [mremap.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 *      linux/mm/remap.c
3
 *
4
 *      (C) Copyright 1996 Linus Torvalds
5
 */
6
 
7
#include <linux/slab.h>
8
#include <linux/smp_lock.h>
9
#include <linux/shm.h>
10
#include <linux/mman.h>
11
#include <linux/swap.h>
12
 
13
#include <asm/uaccess.h>
14
#include <asm/pgalloc.h>
15
 
16
extern int vm_enough_memory(long pages);
17
 
18
static inline pte_t *get_one_pte(struct mm_struct *mm, unsigned long addr)
19
{
20
        pgd_t * pgd;
21
        pmd_t * pmd;
22
        pte_t * pte = NULL;
23
 
24
        pgd = pgd_offset(mm, addr);
25
        if (pgd_none(*pgd))
26
                goto end;
27
        if (pgd_bad(*pgd)) {
28
                pgd_ERROR(*pgd);
29
                pgd_clear(pgd);
30
                goto end;
31
        }
32
 
33
        pmd = pmd_offset(pgd, addr);
34
        if (pmd_none(*pmd))
35
                goto end;
36
        if (pmd_bad(*pmd)) {
37
                pmd_ERROR(*pmd);
38
                pmd_clear(pmd);
39
                goto end;
40
        }
41
 
42
        pte = pte_offset(pmd, addr);
43
        if (pte_none(*pte))
44
                pte = NULL;
45
end:
46
        return pte;
47
}
48
 
49
static inline pte_t *alloc_one_pte(struct mm_struct *mm, unsigned long addr)
50
{
51
        pmd_t * pmd;
52
        pte_t * pte = NULL;
53
 
54
        pmd = pmd_alloc(mm, pgd_offset(mm, addr), addr);
55
        if (pmd)
56
                pte = pte_alloc(mm, pmd, addr);
57
        return pte;
58
}
59
 
60
static inline int copy_one_pte(struct mm_struct *mm, pte_t * src, pte_t * dst)
61
{
62
        int error = 0;
63
        pte_t pte;
64
 
65
        if (!pte_none(*src)) {
66
                pte = ptep_get_and_clear(src);
67
                if (!dst) {
68
                        /* No dest?  We must put it back. */
69
                        dst = src;
70
                        error++;
71
                }
72
                set_pte(dst, pte);
73
        }
74
        return error;
75
}
76
 
77
static int move_one_page(struct mm_struct *mm, unsigned long old_addr, unsigned long new_addr)
78
{
79
        int error = 0;
80
        pte_t * src, * dst;
81
 
82
        spin_lock(&mm->page_table_lock);
83
        src = get_one_pte(mm, old_addr);
84
        if (src) {
85
                dst = alloc_one_pte(mm, new_addr);
86
                src = get_one_pte(mm, old_addr);
87
                if (src)
88
                        error = copy_one_pte(mm, src, dst);
89
        }
90
        spin_unlock(&mm->page_table_lock);
91
        return error;
92
}
93
 
94
static int move_page_tables(struct mm_struct * mm,
95
        unsigned long new_addr, unsigned long old_addr, unsigned long len)
96
{
97
        unsigned long offset = len;
98
 
99
        flush_cache_range(mm, old_addr, old_addr + len);
100
 
101
        /*
102
         * This is not the clever way to do this, but we're taking the
103
         * easy way out on the assumption that most remappings will be
104
         * only a few pages.. This also makes error recovery easier.
105
         */
106
        while (offset) {
107
                offset -= PAGE_SIZE;
108
                if (move_one_page(mm, old_addr + offset, new_addr + offset))
109
                        goto oops_we_failed;
110
        }
111
        flush_tlb_range(mm, old_addr, old_addr + len);
112
        return 0;
113
 
114
        /*
115
         * Ok, the move failed because we didn't have enough pages for
116
         * the new page table tree. This is unlikely, but we have to
117
         * take the possibility into account. In that case we just move
118
         * all the pages back (this will work, because we still have
119
         * the old page tables)
120
         */
121
oops_we_failed:
122
        flush_cache_range(mm, new_addr, new_addr + len);
123
        while ((offset += PAGE_SIZE) < len)
124
                move_one_page(mm, new_addr + offset, old_addr + offset);
125
        zap_page_range(mm, new_addr, len);
126
        return -1;
127
}
128
 
129
static inline unsigned long move_vma(struct vm_area_struct * vma,
130
        unsigned long addr, unsigned long old_len, unsigned long new_len,
131
        unsigned long new_addr)
132
{
133
        struct mm_struct * mm = vma->vm_mm;
134
        struct vm_area_struct * new_vma, * next, * prev;
135
        int allocated_vma;
136
 
137
        new_vma = NULL;
138
        next = find_vma_prev(mm, new_addr, &prev);
139
        if (next) {
140
                if (prev && prev->vm_end == new_addr &&
141
                    can_vma_merge(prev, vma->vm_flags) && !vma->vm_file && !(vma->vm_flags & VM_SHARED)) {
142
                        spin_lock(&mm->page_table_lock);
143
                        prev->vm_end = new_addr + new_len;
144
                        spin_unlock(&mm->page_table_lock);
145
                        new_vma = prev;
146
                        if (next != prev->vm_next)
147
                                BUG();
148
                        if (prev->vm_end == next->vm_start && can_vma_merge(next, prev->vm_flags)) {
149
                                spin_lock(&mm->page_table_lock);
150
                                prev->vm_end = next->vm_end;
151
                                __vma_unlink(mm, next, prev);
152
                                spin_unlock(&mm->page_table_lock);
153
 
154
                                mm->map_count--;
155
                                kmem_cache_free(vm_area_cachep, next);
156
                        }
157
                } else if (next->vm_start == new_addr + new_len &&
158
                           can_vma_merge(next, vma->vm_flags) && !vma->vm_file && !(vma->vm_flags & VM_SHARED)) {
159
                        spin_lock(&mm->page_table_lock);
160
                        next->vm_start = new_addr;
161
                        spin_unlock(&mm->page_table_lock);
162
                        new_vma = next;
163
                }
164
        } else {
165
                prev = find_vma(mm, new_addr-1);
166
                if (prev && prev->vm_end == new_addr &&
167
                    can_vma_merge(prev, vma->vm_flags) && !vma->vm_file && !(vma->vm_flags & VM_SHARED)) {
168
                        spin_lock(&mm->page_table_lock);
169
                        prev->vm_end = new_addr + new_len;
170
                        spin_unlock(&mm->page_table_lock);
171
                        new_vma = prev;
172
                }
173
        }
174
 
175
        allocated_vma = 0;
176
        if (!new_vma) {
177
                new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
178
                if (!new_vma)
179
                        goto out;
180
                allocated_vma = 1;
181
        }
182
 
183
        if (!move_page_tables(current->mm, new_addr, addr, old_len)) {
184
                unsigned long vm_locked = vma->vm_flags & VM_LOCKED;
185
 
186
                if (allocated_vma) {
187
                        *new_vma = *vma;
188
                        new_vma->vm_start = new_addr;
189
                        new_vma->vm_end = new_addr+new_len;
190
                        new_vma->vm_pgoff += (addr-vma->vm_start) >> PAGE_SHIFT;
191
                        new_vma->vm_raend = 0;
192
                        if (new_vma->vm_file)
193
                                get_file(new_vma->vm_file);
194
                        if (new_vma->vm_ops && new_vma->vm_ops->open)
195
                                new_vma->vm_ops->open(new_vma);
196
                        insert_vm_struct(current->mm, new_vma);
197
                }
198
 
199
                do_munmap(current->mm, addr, old_len);
200
 
201
                current->mm->total_vm += new_len >> PAGE_SHIFT;
202
                if (vm_locked) {
203
                        current->mm->locked_vm += new_len >> PAGE_SHIFT;
204
                        if (new_len > old_len)
205
                                make_pages_present(new_addr + old_len,
206
                                                   new_addr + new_len);
207
                }
208
                return new_addr;
209
        }
210
        if (allocated_vma)
211
                kmem_cache_free(vm_area_cachep, new_vma);
212
 out:
213
        return -ENOMEM;
214
}
215
 
216
/*
217
 * Expand (or shrink) an existing mapping, potentially moving it at the
218
 * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
219
 *
220
 * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
221
 * This option implies MREMAP_MAYMOVE.
222
 */
223
unsigned long do_mremap(unsigned long addr,
224
        unsigned long old_len, unsigned long new_len,
225
        unsigned long flags, unsigned long new_addr)
226
{
227
        struct vm_area_struct *vma;
228
        unsigned long ret = -EINVAL;
229
 
230
        if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
231
                goto out;
232
 
233
        if (addr & ~PAGE_MASK)
234
                goto out;
235
 
236
        old_len = PAGE_ALIGN(old_len);
237
        new_len = PAGE_ALIGN(new_len);
238
 
239
        /* new_addr is only valid if MREMAP_FIXED is specified */
240
        if (flags & MREMAP_FIXED) {
241
                if (new_addr & ~PAGE_MASK)
242
                        goto out;
243
                if (!(flags & MREMAP_MAYMOVE))
244
                        goto out;
245
 
246
                if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
247
                        goto out;
248
                /*
249
                 * Allow new_len == 0 only if new_addr == addr
250
                 * to preserve truncation in place (that was working
251
                 * safe and some app may depend on it).
252
                 */
253
                if (unlikely(!new_len && new_addr != addr))
254
                        goto out;
255
 
256
                /* Check if the location we're moving into overlaps the
257
                 * old location at all, and fail if it does.
258
                 */
259
                if ((new_addr <= addr) && (new_addr+new_len) > addr)
260
                        goto out;
261
 
262
                if ((addr <= new_addr) && (addr+old_len) > new_addr)
263
                        goto out;
264
 
265
                ret = do_munmap(current->mm, new_addr, new_len);
266
                if (ret && new_len)
267
                        goto out;
268
        }
269
 
270
        /*
271
         * Always allow a shrinking remap: that just unmaps
272
         * the unnecessary pages..
273
         */
274
        if (old_len >= new_len) {
275
                ret = do_munmap(current->mm, addr+new_len, old_len - new_len);
276
                if (ret && old_len != new_len)
277
                        goto out;
278
                ret = addr;
279
                if (!(flags & MREMAP_FIXED) || (new_addr == addr))
280
                        goto out;
281
        }
282
 
283
        /*
284
         * Ok, we need to grow..  or relocate.
285
         */
286
        ret = -EFAULT;
287
        vma = find_vma(current->mm, addr);
288
        if (!vma || vma->vm_start > addr)
289
                goto out;
290
        /* We can't remap across vm area boundaries */
291
        if (old_len > vma->vm_end - addr)
292
                goto out;
293
        if (vma->vm_flags & VM_DONTEXPAND) {
294
                if (new_len > old_len)
295
                        goto out;
296
        }
297
        if (vma->vm_flags & VM_LOCKED) {
298
                unsigned long locked = current->mm->locked_vm << PAGE_SHIFT;
299
                locked += new_len - old_len;
300
                ret = -EAGAIN;
301
                if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
302
                        goto out;
303
        }
304
        ret = -ENOMEM;
305
        if ((current->mm->total_vm << PAGE_SHIFT) + (new_len - old_len)
306
            > current->rlim[RLIMIT_AS].rlim_cur)
307
                goto out;
308
        /* Private writable mapping? Check memory availability.. */
309
        if ((vma->vm_flags & (VM_SHARED | VM_WRITE)) == VM_WRITE &&
310
            !(flags & MAP_NORESERVE)                             &&
311
            !vm_enough_memory((new_len - old_len) >> PAGE_SHIFT))
312
                goto out;
313
 
314
        /* old_len exactly to the end of the area..
315
         * And we're not relocating the area.
316
         */
317
        if (old_len == vma->vm_end - addr &&
318
            !((flags & MREMAP_FIXED) && (addr != new_addr)) &&
319
            (old_len != new_len || !(flags & MREMAP_MAYMOVE))) {
320
                unsigned long max_addr = TASK_SIZE;
321
                if (vma->vm_next)
322
                        max_addr = vma->vm_next->vm_start;
323
                /* can we just expand the current mapping? */
324
                if (max_addr - addr >= new_len) {
325
                        int pages = (new_len - old_len) >> PAGE_SHIFT;
326
                        spin_lock(&vma->vm_mm->page_table_lock);
327
                        vma->vm_end = addr + new_len;
328
                        spin_unlock(&vma->vm_mm->page_table_lock);
329
                        current->mm->total_vm += pages;
330
                        if (vma->vm_flags & VM_LOCKED) {
331
                                current->mm->locked_vm += pages;
332
                                make_pages_present(addr + old_len,
333
                                                   addr + new_len);
334
                        }
335
                        ret = addr;
336
                        goto out;
337
                }
338
        }
339
 
340
        /*
341
         * We weren't able to just expand or shrink the area,
342
         * we need to create a new one and move it..
343
         */
344
        ret = -ENOMEM;
345
        if (flags & MREMAP_MAYMOVE) {
346
                if (!(flags & MREMAP_FIXED)) {
347
                        unsigned long map_flags = 0;
348
                        if (vma->vm_flags & VM_SHARED)
349
                                map_flags |= MAP_SHARED;
350
 
351
                        new_addr = get_unmapped_area(vma->vm_file, 0, new_len, vma->vm_pgoff, map_flags);
352
                        ret = new_addr;
353
                        if (new_addr & ~PAGE_MASK)
354
                                goto out;
355
                }
356
                ret = move_vma(vma, addr, old_len, new_len, new_addr);
357
        }
358
out:
359
        return ret;
360
}
361
 
362
asmlinkage unsigned long sys_mremap(unsigned long addr,
363
        unsigned long old_len, unsigned long new_len,
364
        unsigned long flags, unsigned long new_addr)
365
{
366
        unsigned long ret;
367
 
368
        down_write(&current->mm->mmap_sem);
369
        ret = do_mremap(addr, old_len, new_len, flags, new_addr);
370
        up_write(&current->mm->mmap_sem);
371
        return ret;
372
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.