2 meinOS - A unix-like x86 microkernel operating system
3 Copyright (C) 2008 Janosch Gräf <janosch.graef@gmx.net>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * Initializes swapping
33 //if (syscall_create(SYSCALL_SWAP_ENABLE,swap_enable,1)==-1) return -1;
38 * Enables swapping (Syscall)
41 int swap_enable(void *buf
) {
42 if (swap_proc
!=NULL
) return -1;
43 swap_queue
= llist_create();
44 swap_proc
= proc_current
;
50 * Calls a swap function
52 * @param proc Owner of page
56 int swap_call(int op
,proc_t
*proc
,void *page
) {
57 swap_call_t
*new = malloc(sizeof(swap_call_t
));
62 llist_push(swap_queue
,new);
70 * @param proc Onwer of page
71 * @param page Page to swap
73 int swap_in(proc_t
*proc
,void *page
) {
74 if (swap_proc
!=NULL
) {
75 pte_t pte
= paging_getpte_pd(page
,proc
->addrspace
->pagedir
);
76 if (!pte
.in_memory
&& pte
.swapped
) {
77 memuser_load_addrspace(proc
->addrspace
);
78 if (swap_call(SWAP_IN
,proc
,page
)) {
79 void *phys
= memphys_alloc();
80 pte
.page
= ADDR2PAGE(phys
);
82 paging_physwrite(phys
,swap_buf
,PAGE_SIZE
);
92 * @param proc Owner of page
93 * @param page Page to swap
96 int swap_out(proc_t
*proc
,void *page
) {
97 if (swap_proc
!=NULL
) {
98 pte_t pte
= paging_getpte_pd(page
,proc
->addrspace
->pagedir
);
100 memuser_load_addrspace(proc
->addrspace
);
101 void *phys
= PAGE2ADDR(pte
.page
);
102 if (phys
!=NULL
&& pte
.in_memory
) {
103 paging_physread(swap_buf
,phys
,PAGE_SIZE
);
104 if (swap_call(SWAP_OUT
,proc
,page
)!=-1) {
108 paging_setpte_pd(page
,pte
,proc
->addrspace
->pagedir
);
118 * Removes a page from swap
119 * @param proc Owner of page
123 int swap_remove(proc_t
*proc
,void *page
) {
124 if (swap_proc
!=NULL
) return swap_call(SWAP_REM
,proc
,page
);