feat: standard vga support (mode switching, framebuffer remapping)
[lunaix-os.git] / lunaix-os / kernel / fs / pcache.c
1 #include <klibc/string.h>
2 #include <lunaix/ds/btrie.h>
3 #include <lunaix/fs.h>
4 #include <lunaix/mm/page.h>
5 #include <lunaix/mm/pmm.h>
6 #include <lunaix/mm/valloc.h>
7 #include <lunaix/mm/vmm.h>
8 #include <lunaix/spike.h>
9
10 #define PCACHE_DIRTY 0x1
11
12 static struct lru_zone* pcache_zone;
13
14 static int
15 __pcache_try_evict(struct lru_node* obj)
16 {
17     struct pcache_pg* page = container_of(obj, struct pcache_pg, lru);
18     pcache_invalidate(page->holder, page);
19     return 1;
20 }
21
22 static void
23 pcache_free_page(void* va)
24 {
25     ptr_t pa = vmm_del_mapping(VMS_SELF, (ptr_t)va);
26     pmm_free_page(KERNEL_PID, pa);
27 }
28
29 static void*
30 pcache_alloc_page()
31 {
32     int i = 0;
33     ptr_t pp = pmm_alloc_page(KERNEL_PID, 0), va = 0;
34
35     if (!pp) {
36         return NULL;
37     }
38
39     if (!(va = (ptr_t)vmap(pp, PG_SIZE, PG_PREM_RW, 0))) {
40         pmm_free_page(KERNEL_PID, pp);
41         return NULL;
42     }
43
44     return (void*)va;
45 }
46
47 void
48 pcache_init(struct pcache* pcache)
49 {
50     btrie_init(&pcache->tree, PG_SIZE_BITS);
51     llist_init_head(&pcache->dirty);
52     llist_init_head(&pcache->pages);
53
54     pcache_zone = lru_new_zone(__pcache_try_evict);
55 }
56
57 void
58 pcache_release_page(struct pcache* pcache, struct pcache_pg* page)
59 {
60     pcache_free_page(page->pg);
61
62     llist_delete(&page->pg_list);
63
64     btrie_remove(&pcache->tree, page->fpos);
65
66     vfree(page);
67
68     pcache->n_pages--;
69 }
70
71 struct pcache_pg*
72 pcache_new_page(struct pcache* pcache, u32_t index)
73 {
74     struct pcache_pg* ppg = vzalloc(sizeof(struct pcache_pg));
75     void* pg = pcache_alloc_page();
76
77     if (!ppg || !pg) {
78         lru_evict_one(pcache_zone);
79         if (!ppg && !(ppg = vzalloc(sizeof(struct pcache_pg)))) {
80             return NULL;
81         }
82
83         if (!pg && !(pg = pcache_alloc_page())) {
84             return NULL;
85         }
86     }
87
88     ppg->pg = pg;
89     ppg->holder = pcache;
90
91     llist_append(&pcache->pages, &ppg->pg_list);
92     btrie_set(&pcache->tree, index, ppg);
93
94     return ppg;
95 }
96
97 void
98 pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg)
99 {
100     if (!(pg->flags & PCACHE_DIRTY)) {
101         pg->flags |= PCACHE_DIRTY;
102         pcache->n_dirty++;
103         llist_append(&pcache->dirty, &pg->dirty_list);
104     }
105 }
106
107 int
108 pcache_get_page(struct pcache* pcache,
109                 u32_t index,
110                 u32_t* offset,
111                 struct pcache_pg** page)
112 {
113     struct pcache_pg* pg = btrie_get(&pcache->tree, index);
114     int is_new = 0;
115     u32_t mask = ((1 << pcache->tree.truncated) - 1);
116     *offset = index & mask;
117     if (!pg && (pg = pcache_new_page(pcache, index))) {
118         pg->fpos = index & ~mask;
119         pcache->n_pages++;
120         is_new = 1;
121     }
122     if (pg)
123         lru_use_one(pcache_zone, &pg->lru);
124     *page = pg;
125     return is_new;
126 }
127
128 int
129 pcache_write(struct v_inode* inode, void* data, u32_t len, u32_t fpos)
130 {
131     int errno = 0;
132     u32_t pg_off, buf_off = 0;
133     struct pcache* pcache = inode->pg_cache;
134     struct pcache_pg* pg;
135
136     while (buf_off < len && errno >= 0) {
137         u32_t wr_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
138
139         int new_page = pcache_get_page(pcache, fpos, &pg_off, &pg);
140
141         if (new_page) {
142             // Filling up the page
143             errno =
144               inode->default_fops->read_page(inode, pg->pg, PG_SIZE, pg->fpos);
145
146             if (errno < 0) {
147                 break;
148             }
149             if (errno < PG_SIZE) {
150                 // EOF
151                 len = MIN(len, buf_off + errno);
152             }
153         } else if (!pg) {
154             errno = inode->default_fops->write(inode, data, wr_bytes, fpos);
155             continue;
156         }
157
158         memcpy(pg->pg + pg_off, (data + buf_off), wr_bytes);
159         pcache_set_dirty(pcache, pg);
160
161         pg->len = pg_off + wr_bytes;
162         buf_off += wr_bytes;
163         fpos += wr_bytes;
164     }
165
166     return errno < 0 ? errno : (int)buf_off;
167 }
168
169 int
170 pcache_read(struct v_inode* inode, void* data, u32_t len, u32_t fpos)
171 {
172     u32_t pg_off, buf_off = 0, new_pg = 0;
173     int errno = 0;
174     struct pcache* pcache = inode->pg_cache;
175     struct pcache_pg* pg;
176
177     while (buf_off < len) {
178         int new_page = pcache_get_page(pcache, fpos, &pg_off, &pg);
179         if (new_page) {
180             // Filling up the page
181             errno =
182               inode->default_fops->read_page(inode, pg->pg, PG_SIZE, pg->fpos);
183
184             if (errno < 0) {
185                 break;
186             }
187             if (errno < PG_SIZE) {
188                 // EOF
189                 len = MIN(len, buf_off + errno);
190             }
191
192             pg->len = errno;
193         } else if (!pg) {
194             errno = inode->default_fops->read_page(
195               inode, (data + buf_off), len - buf_off, pg->fpos);
196             buf_off = len;
197             break;
198         }
199
200         u32_t rd_bytes = MIN(pg->len - pg_off, len - buf_off);
201
202         if (!rd_bytes)
203             break;
204
205         memcpy((data + buf_off), pg->pg + pg_off, rd_bytes);
206
207         buf_off += rd_bytes;
208         fpos += rd_bytes;
209     }
210
211     return errno < 0 ? errno : (int)buf_off;
212 }
213
214 void
215 pcache_release(struct pcache* pcache)
216 {
217     struct pcache_pg *pos, *n;
218     llist_for_each(pos, n, &pcache->pages, pg_list)
219     {
220         lru_remove(pcache_zone, &pos->lru);
221         vfree(pos);
222     }
223
224     btrie_release(&pcache->tree);
225 }
226
227 int
228 pcache_commit(struct v_inode* inode, struct pcache_pg* page)
229 {
230     if (!(page->flags & PCACHE_DIRTY)) {
231         return 0;
232     }
233
234     int errno =
235       inode->default_fops->write_page(inode, page->pg, PG_SIZE, page->fpos);
236
237     if (!errno) {
238         page->flags &= ~PCACHE_DIRTY;
239         llist_delete(&page->dirty_list);
240         inode->pg_cache->n_dirty--;
241     }
242
243     return errno;
244 }
245
246 void
247 pcache_commit_all(struct v_inode* inode)
248 {
249     if (!inode->pg_cache) {
250         return;
251     }
252
253     struct pcache* cache = inode->pg_cache;
254     struct pcache_pg *pos, *n;
255
256     llist_for_each(pos, n, &cache->dirty, dirty_list)
257     {
258         pcache_commit(inode, pos);
259     }
260 }
261
262 void
263 pcache_invalidate(struct pcache* pcache, struct pcache_pg* page)
264 {
265     pcache_commit(pcache->master, page);
266     pcache_release_page(pcache, page);
267 }