forked from sysudengle/OS_Page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagetable.h
108 lines (85 loc) · 3.32 KB
/
pagetable.h
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
#ifndef __PAGETABLE_H__
#define __PAGETABLE_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define TRACE_64
#define PAGE_SHIFT 12 // number of bits 2^(PAGE_SHIFT) == PAGE_SIZE
#define PAGE_SIZE 4096 // Size of pagetable pages
#define PAGE_MASK (~(PAGE_SIZE-1))
#define PG_VALID (0x1) // Valid bit in pgd or pte, set if in memory
#define PG_DIRTY (0x2) // Dirty bit in pgd or pte, set if modified
#define PG_REF (0x4) // Reference bit, set if page has been referenced
#define PG_ONSWAP (0x8) // Set if page has been evicted to swap
#define INVALID_SWAP -1
#ifdef TRACE_64
// User-level virtual addresses on 64-bit Linux system are 36 bits in our traces
// and the page size is still 4096 (12 bits).
// We split the remaining 24 bits evenly into top-level (page directory) index
// and second-level (page table) index, using 12 bits for each.
#define PGDIR_SHIFT 24 // Leaves just top 12 bits of vaddr
#define PTRS_PER_PGDIR 4096
#define PTRS_PER_PGTBL 4096
#else // TRACE_32
// User-level virtual addresses on 32-bit Linux system are 32 bits, and the
// page size is still 4096 (12 bits).
// We split the remaining 20 bits evenly into top-level (page directory) index
// and second level (page table) index, using 10 bits for each.
#define PGDIR_SHIFT 22 // Leaves just top 10 bits of vaddr
#define PTRS_PER_PGDIR 1024
#define PTRS_PER_PGTBL 1024
#endif
#define PGTBL_MASK (PTRS_PER_PGTBL-1)
#define PGDIR_INDEX(x) ((x) >> PGDIR_SHIFT)
#define PGTBL_INDEX(x) (((x) >> PAGE_SHIFT) & PGTBL_MASK)
typedef unsigned long addr_t;
// These defines allow us to take advantage of the compiler's typechecking
// Page directory entry (top-level)
typedef struct {
// Pointer, int type
uintptr_t pde;
} pgdir_entry_t;
// Page table entry (2nd-level).
// 第二层元素
typedef struct {
unsigned int frame; // if valid bit == 1, physical frame holding vpage
off_t swap_off; // offset in swap file of vpage, if any
unsigned long stamp;
int ref;
} pgtbl_entry_t;
extern void init_pagetable();
extern char *find_physpage(addr_t vaddr, char type);
extern void print_pagedirectory(void);
//物理内存
struct frame {
char in_use; // True if frame is allocated, False if frame is free
pgtbl_entry_t *pte;// Pointer back to pagetable entry (pte) for page
// stored in this frame
};
/* The coremap holds information about physical memory.
* The index into coremap is the physical page frame number stored
* in the page table entry (pgtbl_entry_t).
*/
extern struct frame *coremap;
// Swap functions for use in other files
extern int swap_init(unsigned swapsize);
extern void swap_destroy(void);
extern int swap_pagein(unsigned frame, int swap_offset);
extern int swap_pageout(unsigned frame, int swap_offset);
extern void rand_init();
extern void lru_init();
extern void clock_init();
extern void fifo_init();
extern void opt_init();
// These may not need to do anything for some algorithms
extern void rand_ref(pgtbl_entry_t *);
extern void lru_ref(pgtbl_entry_t *);
extern void clock_ref(pgtbl_entry_t *);
extern void fifo_ref(pgtbl_entry_t *);
extern void opt_ref(pgtbl_entry_t *);
extern int rand_evict();
extern int lru_evict();
extern int clock_evict();
extern int fifo_evict();
extern int opt_evict();
#endif /* PAGETABLE_H */