xv6-simplified 0.1
简化版xv6
Loading...
Searching...
No Matches
Functions | Variables
vm.c File Reference

虚拟内存管理的实现,包括创建和管理页表,以及将虚拟地址映射到物理地址 More...

#include "include/param.h"
#include "include/types.h"
#include "include/memlayout.h"
#include "include/elf.h"
#include "include/riscv.h"
#include "include/defs.h"
Include dependency graph for vm.c:

Functions

pagetable_t kvmmake (void)
 创建一个内核页表,其中包含了一些内核需要的映射关系,是内核能够访问到这些区域的内容
 
void kvminit (void)
 初始化内核页表kernel_pagetable
 
void kvminithart ()
 将硬件页表寄存器切换到内核页表,并启用分页
 
pte_twalk (pagetable_t pagetable, uint64 va, int alloc)
 在页表中查找虚拟地址va对应的页表项,如果页表项不存在则根据alloc参数决定是否分配新的页表
 
uint64 walkaddr (pagetable_t pagetable, uint64 va)
 查找虚拟地址对应的物理地址,如果没有映射则返回0,它只能用于查找用户页
 
void kvmmap (pagetable_t kpgtbl, uint64 va, uint64 pa, uint64 sz, int perm)
 将虚拟地址范围映射到物理地址,并将相应的PTE添加到页表中
 
int mappages (pagetable_t pagetable, uint64 va, uint64 size, uint64 pa, int perm)
 从va开始的虚拟地址创建PTE,这些PTE指向从pa开始的物理地址
 
void uvmunmap (pagetable_t pagetable, uint64 va, uint64 npages, int do_free)
 从虚拟地址va开始,移除npages页的映射,va必须是页对齐的,映射必须存在
 
pagetable_t uvmcreate ()
 创建一个空的用户页表
 
void uvmfirst (pagetable_t pagetable, uchar *src, uint sz)
 将用户的initcode加载到进程的地址0处,用于第一个进程
 
uint64 uvmalloc (pagetable_t pagetable, uint64 oldsz, uint64 newsz, int xperm)
 为进程从oldsz到newsz分配PTE和物理内存
 
uint64 uvmdealloc (pagetable_t pagetable, uint64 oldsz, uint64 newsz)
 释放用户进程的内存页,以将进程大小从oldsz缩小到newsz
 
void freewalk (pagetable_t pagetable)
 递归地释放页表页,所有映射必须已经被删除
 
void uvmfree (pagetable_t pagetable, uint64 sz)
 释放用户进程的内存页和页表页
 
int uvmcopy (pagetable_t old, pagetable_t new, uint64 sz)
 将一个进程的虚拟地址空间复制到另一个进程的虚拟地址空间,包括页表和物理内存
 
void uvmclear (pagetable_t pagetable, uint64 va)
 将一个页表项标记为无效,使得用户无法访问该页。它通常被用于为用户栈设置保护页
 
int copyout (pagetable_t pagetable, uint64 dstva, char *src, uint64 len)
 从内核空间复制数据到用户空间
 
int copyin (pagetable_t pagetable, char *dst, uint64 srcva, uint64 len)
 从用户空间复制数据到内核空间
 
int copyinstr (pagetable_t pagetable, char *dst, uint64 srcva, uint64 max)
 从用户空间复制一个以'\0'结尾的字符串到内核空间
 

Variables

pagetable_t kernel_pagetable
 
char etext []
 
char trampoline []
 

Detailed Description

虚拟内存管理的实现,包括创建和管理页表,以及将虚拟地址映射到物理地址

Date
23.05.04
Author
lyp
Version
0.1 @status Stable

Function Documentation

◆ copyin()

int copyin ( pagetable_t  pagetable,
char *  dst,
uint64  srcva,
uint64  len 
)

从用户空间复制数据到内核空间

Parameters
pagetable页表
dst目标内存地址
srcva源内存地址
len要复制的数据长度
Returns
0:成功 -1:失败
Here is the call graph for this function:
Here is the caller graph for this function:

◆ copyinstr()

int copyinstr ( pagetable_t  pagetable,
char *  dst,
uint64  srcva,
uint64  max 
)

从用户空间复制一个以'\0'结尾的字符串到内核空间

Parameters
pagetable页表
dst目标内存地址
srcva源内存地址
max最大复制字节数
Returns
0:成功 -1:失败
Here is the call graph for this function:
Here is the caller graph for this function:

◆ copyout()

int copyout ( pagetable_t  pagetable,
uint64  dstva,
char *  src,
uint64  len 
)

从内核空间复制数据到用户空间

Parameters
pagetable用户进程的页表
dstva目标虚拟地址
src源数据的指针
len要复制的数据长度
Returns
0:成功 -1:失败
Here is the call graph for this function:
Here is the caller graph for this function:

◆ freewalk()

void freewalk ( pagetable_t  pagetable)

递归地释放页表页,所有映射必须已经被删除

Parameters
pagetable页表
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvminit()

void kvminit ( void  )

初始化内核页表kernel_pagetable

Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvminithart()

void kvminithart ( void  )

将硬件页表寄存器切换到内核页表,并启用分页

Here is the caller graph for this function:

◆ kvmmake()

pagetable_t kvmmake ( void  )

创建一个内核页表,其中包含了一些内核需要的映射关系,是内核能够访问到这些区域的内容

Returns
内核页表
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvmmap()

void kvmmap ( pagetable_t  kpgtbl,
uint64  va,
uint64  pa,
uint64  sz,
int  perm 
)

将虚拟地址范围映射到物理地址,并将相应的PTE添加到页表中

Parameters
kpgtbl内核页表的起始地址
va起始虚拟地址
pa起始物理地址
sz映射的大小
perm相应的权限
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mappages()

int mappages ( pagetable_t  pagetable,
uint64  va,
uint64  size,
uint64  pa,
int  perm 
)

从va开始的虚拟地址创建PTE,这些PTE指向从pa开始的物理地址

Parameters
pagetable页表的起始地址
va起始虚拟地址
size映射的大小
pa起始物理地址
perm相应的权限
Returns
0:创建成功 -1:walk()无法分配页表
Here is the call graph for this function:
Here is the caller graph for this function:

◆ uvmalloc()

uint64 uvmalloc ( pagetable_t  pagetable,
uint64  oldsz,
uint64  newsz,
int  xperm 
)

为进程从oldsz到newsz分配PTE和物理内存

Parameters
pagetable进程的页表
oldsz进程的旧大小
newsz进程的新大小
xperm权限
Returns
0:分配失败 newsz:分配成功
Here is the call graph for this function:
Here is the caller graph for this function:

◆ uvmclear()

void uvmclear ( pagetable_t  pagetable,
uint64  va 
)

将一个页表项标记为无效,使得用户无法访问该页。它通常被用于为用户栈设置保护页

Parameters
pagetable用户进程的页表
va虚拟地址
Here is the call graph for this function:
Here is the caller graph for this function:

◆ uvmcopy()

int uvmcopy ( pagetable_t  old,
pagetable_t  new,
uint64  sz 
)

将一个进程的虚拟地址空间复制到另一个进程的虚拟地址空间,包括页表和物理内存

Parameters
old源进程的页表
new目标进程的页表
sz源进程的大小
Returns
0:成功 -1:失败,并释放已映射的物理页
Here is the call graph for this function:

◆ uvmcreate()

pagetable_t uvmcreate ( )

创建一个空的用户页表

Returns
返回页表的指针,如果内存不足返回0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ uvmdealloc()

uint64 uvmdealloc ( pagetable_t  pagetable,
uint64  oldsz,
uint64  newsz 
)

释放用户进程的内存页,以将进程大小从oldsz缩小到newsz

Parameters
pagetable进程的页表
oldsz进程的旧大小
newsz进程的新大小
Returns
新进程的大小
Here is the call graph for this function:
Here is the caller graph for this function:

◆ uvmfirst()

void uvmfirst ( pagetable_t  pagetable,
uchar src,
uint  sz 
)

将用户的initcode加载到进程的地址0处,用于第一个进程

Parameters
pagetable进程的页表
src指向用户initcode的指针
sz用户initcode的大小,必须小于一页
Here is the call graph for this function:
Here is the caller graph for this function:

◆ uvmfree()

void uvmfree ( pagetable_t  pagetable,
uint64  sz 
)

释放用户进程的内存页和页表页

Parameters
pagetable用户进程的页表
sz用户进程的大小
Here is the call graph for this function:
Here is the caller graph for this function:

◆ uvmunmap()

void uvmunmap ( pagetable_t  pagetable,
uint64  va,
uint64  npages,
int  do_free 
)

从虚拟地址va开始,移除npages页的映射,va必须是页对齐的,映射必须存在

Parameters
pagetable页表
va虚拟地址
npages要移除的页数
do_free是否释放物理内存
Here is the call graph for this function:
Here is the caller graph for this function:

◆ walk()

pte_t * walk ( pagetable_t  pagetable,
uint64  va,
int  alloc 
)

在页表中查找虚拟地址va对应的页表项,如果页表项不存在则根据alloc参数决定是否分配新的页表

Parameters
pagetable页表的起始地址
va所需查找的虚拟地址
alloc是否需要分配新的页表项
Returns
所指页表项的指针,若未找到则返回0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ walkaddr()

uint64 walkaddr ( pagetable_t  pagetable,
uint64  va 
)

查找虚拟地址对应的物理地址,如果没有映射则返回0,它只能用于查找用户页

Parameters
pagetable页表
va虚拟地址
Returns
物理地址
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ etext

char etext[]
extern

◆ kernel_pagetable

pagetable_t kernel_pagetable

◆ trampoline

char trampoline[]
extern