Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
pintos-public
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alexandru Dura
pintos-public
Commits
624005bd
Commit
624005bd
authored
Apr 16, 2020
by
Flavius Gruian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added swap for Lab4.
parent
22093991
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
0 deletions
+67
-0
src/vm/swap.c
src/vm/swap.c
+67
-0
No files found.
src/vm/swap.c
0 → 100644
View file @
624005bd
#include "vm/swap.h"
#include "bitmap.h"
#include "threads/synch.h"
#include "threads/vaddr.h"
#include "devices/block.h"
static
struct
block
*
swap_partition
;
// this might not need to be saved here
static
struct
bitmap
*
swap_freemap
;
static
struct
lock
swap_lock
;
#define SECTORS_PER_PAGE (PGSIZE/BLOCK_SECTOR_SIZE)
/* this must be called at the right time */
void
swap_init
()
{
block_print_stats
();
swap_partition
=
block_get_role
(
BLOCK_SWAP
);
if
(
swap_partition
)
{
unsigned
npgswp
=
block_size
(
swap_partition
)
/
SECTORS_PER_PAGE
;
printf
(
"Swap size is %d pages.
\n
"
,
npgswp
);
swap_freemap
=
bitmap_create
(
npgswp
);
bitmap_set_all
(
swap_freemap
,
true
);
}
lock_init
(
&
swap_lock
);
}
/* returns the index of the swap page storing the given memory page */
size_t
swap_out
(
const
void
*
kpage
)
{
size_t
index
=
BITMAP_ERROR
;
lock_acquire
(
&
swap_lock
);
index
=
bitmap_scan_and_flip
(
swap_freemap
,
0
,
1
,
true
);
if
(
index
!=
BITMAP_ERROR
)
{
block_sector_t
swap_offset
=
index
*
SECTORS_PER_PAGE
;
for
(
block_sector_t
i
=
0
;
i
<
SECTORS_PER_PAGE
;
i
++
)
{
// printf("swap out %p to %d\n", kpage+i*BLOCK_SECTOR_SIZE, swap_offset+i);
block_write
(
swap_partition
,
swap_offset
+
i
,
kpage
+
i
*
BLOCK_SECTOR_SIZE
);
}
}
lock_release
(
&
swap_lock
);
return
index
;
}
bool
swap_in
(
size_t
page_index
,
void
*
kpage
)
{
bool
ok
=
false
;
lock_acquire
(
&
swap_lock
);
if
(
bitmap_contains
(
swap_freemap
,
page_index
,
1
,
false
))
{
/* the swap page contains something (not free) */
block_sector_t
swap_offset
=
page_index
*
SECTORS_PER_PAGE
;
for
(
block_sector_t
i
=
0
;
i
<
SECTORS_PER_PAGE
;
i
++
)
{
// printf("swap in %d to %p\n", swap_offset+i, kpage+i*BLOCK_SECTOR_SIZE);
block_read
(
swap_partition
,
swap_offset
+
i
,
kpage
+
i
*
BLOCK_SECTOR_SIZE
);
}
ok
=
true
;
}
lock_release
(
&
swap_lock
);
return
ok
;
}
void
swap_free
(
size_t
page_index
)
{
// lock_acquire(&swap_lock);
bitmap_mark
(
swap_freemap
,
page_index
);
// this is already atomic
// lock_release(&swap_lock);
}
void
swap_destroy
()
{
ASSERT
(
swap_freemap
);
bitmap_destroy
(
swap_freemap
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment