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

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [posix/] [mm0/] [mm/] [vm_object.c] - Blame information for rev 7

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

Line No. Rev Author Line
1 2 drasko
/*
2
 * vm object utility functions.
3
 *
4
 * Copyright (C) 2008 Bahadir Balban
5
 */
6
#include <file.h>
7
#include <vm_area.h>
8
#include <l4/macros.h>
9
#include <l4/api/errno.h>
10
#include <malloc/malloc.h>
11
#include <globals.h>
12
 
13
/* Global list of all in-memory files on the system */
14
struct global_list global_vm_files = {
15
        .list = { &global_vm_files.list, &global_vm_files.list },
16
        .total = 0,
17
};
18
 
19
/* Global list of in-memory vm objects in the system */
20
struct global_list global_vm_objects = {
21
        .list = { &global_vm_objects.list, &global_vm_objects.list },
22
        .total = 0,
23
};
24
 
25
 
26
void global_add_vm_object(struct vm_object *obj)
27
{
28
        BUG_ON(!list_empty(&obj->list));
29
        list_insert(&obj->list, &global_vm_objects.list);
30
        global_vm_objects.total++;
31
}
32
 
33
void global_remove_vm_object(struct vm_object *obj)
34
{
35
        BUG_ON(list_empty(&obj->list));
36
        list_remove_init(&obj->list);
37
        BUG_ON(--global_vm_objects.total < 0);
38
}
39
 
40
void global_add_vm_file(struct vm_file *f)
41
{
42
        BUG_ON(!list_empty(&f->list));
43
        list_insert(&f->list, &global_vm_files.list);
44
        global_vm_files.total++;
45
 
46
        global_add_vm_object(&f->vm_obj);
47
}
48
 
49
void global_remove_vm_file(struct vm_file *f)
50
{
51
        BUG_ON(list_empty(&f->list));
52
        list_remove_init(&f->list);
53
        BUG_ON(--global_vm_files.total < 0);
54
 
55
        global_remove_vm_object(&f->vm_obj);
56
}
57
 
58
void print_cache_pages(struct vm_object *vmo)
59
{
60
        struct page *p;
61
 
62
        if (!list_empty(&vmo->page_cache))
63
                printf("Pages:\n======\n");
64
 
65
        list_foreach_struct(p, &vmo->page_cache, list) {
66
                dprintf("Page offset: 0x%lx, virtual: 0x%lx, refcnt: %d\n", p->offset,
67
                       p->virtual, p->refcnt);
68
        }
69
}
70
 
71
void vm_object_print(struct vm_object *vmo)
72
{
73
        struct vm_file *f;
74
 
75
        printf("Object type: %s %s. links: %d, shadows: %d, Pages in cache: %d.\n",
76
               vmo->flags & VM_WRITE ? "writeable" : "read-only",
77
               vmo->flags & VM_OBJ_FILE ? "file" : "shadow", vmo->nlinks, vmo->shadows,
78
               vmo->npages);
79
        if (vmo->flags & VM_OBJ_FILE) {
80
                f = vm_object_to_file(vmo);
81
                char *ftype;
82
 
83
                if (f->type == VM_FILE_DEVZERO)
84
                        ftype = "devzero";
85
                else if (f->type == VM_FILE_SHM)
86
                        ftype = "shm file";
87
                else if (f->type == VM_FILE_VFS)
88
                        ftype = "regular";
89
                else
90
                        BUG();
91
 
92
                printf("File type: %s\n", ftype);
93
        }
94
        // print_cache_pages(vmo);
95
        // printf("\n");
96
}
97
 
98
void vm_print_files(struct link *files)
99
{
100
        struct vm_file *f;
101
 
102
        list_foreach_struct(f, files, list)
103
                vm_object_print(&f->vm_obj);
104
}
105
 
106
void vm_print_objects(struct link *objects)
107
{
108
        struct vm_object *vmo;
109
 
110
        list_foreach_struct(vmo, objects, list)
111
                vm_object_print(vmo);
112
}
113
 
114
struct vm_object *vm_object_init(struct vm_object *obj)
115
{
116
        link_init(&obj->list);
117
        link_init(&obj->shref);
118
        link_init(&obj->shdw_list);
119
        link_init(&obj->page_cache);
120
        link_init(&obj->link_list);
121
 
122
        return obj;
123
}
124
 
125
/* Allocate and initialise a vmfile, and return it */
126
struct vm_object *vm_object_create(void)
127
{
128
        struct vm_object *obj;
129
 
130
        if (!(obj = kzalloc(sizeof(*obj))))
131
                return 0;
132
 
133
        return vm_object_init(obj);
134
}
135
 
136
struct vm_file *vm_file_create(void)
137
{
138
        struct vm_file *f;
139
 
140
        if (!(f = kzalloc(sizeof(*f))))
141
                return PTR_ERR(-ENOMEM);
142
 
143
        link_init(&f->list);
144
        vm_object_init(&f->vm_obj);
145
        f->vm_obj.flags = VM_OBJ_FILE;
146
 
147
        return f;
148
}
149
 
150
/*
151
 * Populates the priv_data with vfs-file-specific
152
 * information.
153
 */
154
struct vm_file *vfs_file_create(void)
155
{
156
        struct vm_file *f = vm_file_create();
157
 
158
        if (IS_ERR(f))
159
                return f;
160
 
161
        f->type = VM_FILE_VFS;
162
 
163
        return f;
164
}
165
 
166
/* Deletes the object via its base, along with all its pages */
167
int vm_object_delete(struct vm_object *vmo)
168
{
169
        struct vm_file *f;
170
 
171
        // vm_object_print(vmo);
172
 
173
        /* Release all pages */
174
        vmo->pager->ops.release_pages(vmo);
175
 
176
        /* Remove from global list */
177
        if (vmo->flags & VM_OBJ_FILE)
178
                global_remove_vm_file(vm_object_to_file(vmo));
179
        else if (vmo->flags & VM_OBJ_SHADOW)
180
                global_remove_vm_object(vmo);
181
        else BUG();
182
 
183
        /* Check any references */
184
        BUG_ON(vmo->nlinks);
185
        BUG_ON(vmo->shadows);
186
        BUG_ON(!list_empty(&vmo->shdw_list));
187
        BUG_ON(!list_empty(&vmo->link_list));
188
        BUG_ON(!list_empty(&vmo->page_cache));
189
        BUG_ON(!list_empty(&vmo->shref));
190
 
191
        /* Obtain and free via the base object */
192
        if (vmo->flags & VM_OBJ_FILE) {
193
                f = vm_object_to_file(vmo);
194
                BUG_ON(!list_empty(&f->list));
195
                if (f->private_file_data) {
196
                        if (f->destroy_priv_data)
197
                                f->destroy_priv_data(f);
198
                        else
199
                                kfree(f->private_file_data);
200
                }
201
                kfree(f);
202
        } else if (vmo->flags & VM_OBJ_SHADOW)
203
                kfree(vmo);
204
        else BUG();
205
 
206
        return 0;
207
}
208
 
209
int vm_file_delete(struct vm_file *f)
210
{
211
        /* Delete file via base object */
212
        return vm_object_delete(&f->vm_obj);
213
}
214
 

powered by: WebSVN 2.1.0

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