aboutsummaryrefslogtreecommitdiffstats
path: root/xenolinux-2.4.16-sparse/arch/xeno/mm/get_unmapped_area.c
blob: a7b44475892cc195011fa67efdab5408ceeae8af (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
#include <linux/slab.h>
#include <linux/shm.h>
#include <linux/mman.h>
#include <linux/pagemap.h>
#include <linux/swap.h>
#include <linux/swapctl.h>
#include <linux/smp_lock.h>
#include <linux/init.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/personality.h>

#include <asm/uaccess.h>
#include <asm/pgalloc.h>

/*
static int direct_mapped(unsigned long addr)
{
    direct_mmap_node_t * node;
    struct list_head * curr;
    struct list_head * direct_list = &current->mm->context.direct_list;

    curr = direct_list->next;
    while(curr != direct_list){
        node = list_entry(curr, direct_mmap_node_t, list);
        if(node->addr == addr)
            break;
        curr = curr->next;
    } 

    if(curr == direct_list)
        return 0;

    return 1;
}
*/
/*
unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags)
{
	struct vm_area_struct *vma;

	if (len > TASK_SIZE)
		return -ENOMEM;

	if (addr) {
		addr = PAGE_ALIGN(addr);
		vma = find_vma(current->mm, addr);
		if (TASK_SIZE - len >= addr &&
		    (!vma || addr + len <= vma->vm_start))
			return addr;
	}
	addr = PAGE_ALIGN(TASK_UNMAPPED_BASE);

	for (vma = find_vma(current->mm, addr); ; vma = vma->vm_next) {
		if (TASK_SIZE - len < addr)
			return -ENOMEM;
        
		if ((!vma || addr + len <= vma->vm_start) && !direct_mapped(addr))
			return addr;
		
        addr = vma->vm_end;
	}
}
*/
struct list_head *find_direct(struct list_head *list, unsigned long addr)
{
	struct list_head * curr;
	struct list_head * direct_list = &current->mm->context.direct_list;
	direct_mmap_node_t * node;

    for ( curr = direct_list->next; curr != direct_list; curr = curr->next )
    {
        node = list_entry(curr, direct_mmap_node_t, list);
        if ( node->vm_start >= addr ) break;
    }

    return curr;
}

unsigned long arch_get_unmapped_area(struct file *filp, unsigned long
addr, unsigned long len, unsigned long pgoff, unsigned long flags)
{
    struct vm_area_struct *vma;
    direct_mmap_node_t * node;
    struct list_head * curr;
    struct list_head * direct_list = &current->mm->context.direct_list;

    if (len > TASK_SIZE)
        return -ENOMEM;

    if ( addr )
    {
        addr = PAGE_ALIGN(addr);
        vma = find_vma(current->mm, addr);
        curr = find_direct(direct_list, addr);
        node = list_entry(curr, direct_mmap_node_t, list);
        if ( (TASK_SIZE - len >= addr) &&
             (!vma || addr + len <= vma->vm_start) &&
             ((curr == direct_list) || addr + len <= node->vm_start) )
            return addr;
    }

    addr = PAGE_ALIGN(TASK_UNMAPPED_BASE);


    /* Find first VMA and direct_map nodes with vm_start > addr */
    vma  = find_vma(current->mm, addr);
    curr = find_direct(direct_list, addr);
    node = list_entry(curr, direct_mmap_node_t, list);

    for ( ; ; )
    {
        if ( TASK_SIZE - len < addr ) return -ENOMEM;

        if ( vma && ((curr == direct_list) || (vma->vm_start < node->vm_start)))
        {
            /* Do we fit before VMA node? */
            if ( addr + len <= vma->vm_start ) return addr;
            addr = vma->vm_end;
            vma = vma->vm_next;
        }
        else if ( curr != direct_list )
        {
            /* Do we fit before direct_map node? */
            if ( addr + len <= node->vm_start) return addr;
            addr = node->vm_end;
            curr = curr->next;
            node = list_entry(curr, direct_mmap_node_t, list);
        }
        else
        {
            /* !vma && curr == direct_list */
            return addr;
        }
    }
}