c.part ,c.elifsys ,h.corp ,c.corp :selif ruof niatnoc dluohs
redlof ecruos ehT .trap sunob eht rof redlof cificeps a deen t’nod uoy ,tnemngissa siht roF
.noitcuded edarg esuac dluow hctamsim erutcurtS .enif si erutcurts
ruoy fi kcehc ot dnammoc esu osla nac uoY .woleb detartsulli si erutcurts tcejorp ehT
ediug tamroF
p77=7.0*))sunob( 01+001( teg lliw eH .m.a
01:2 ta tpmetta tcefrep a dettimbus auH iL ,.g.e( .no os dna %03 yb decudeR :ma 00:3-ma
10:2 ;%02 yb decudeR :ma 00:2-ma 10:1 ;%01 yb decudeR :ma 00:1-ma 61:00 tuB .sedarg
ruoy no ytlanep yna ecudni ton lliw setunim 51 nihtiw noissimbus etal A :noissimbus etaL
.srotceted msiraigalp gnisu tenretni eht no elbaliava snoitulos
dna edoc ‘stneduts wollef ruoy ot ralimis oot si margorp ruoy rehtehw kcehc dluow ew
taht eton osla esaelP .flesruoy yb nettirw deedni era sedoc eht taht erusne ot margorp ruoy
fo gninaem eht nialpxe ot uoy ksa yam sAT taht eton esaelP .neddibrof yltcirts si msiraigalP
3202 voN 51 ,95:32 :no euD
noissimbuS
.draobkcalB no
elbaliava si egami ehT .tes dna derugifnoc si gnihtyreve erehw egami enihcam lautriv a edivorp
eW .llac metsys ‘)(pamnum’ eht gnisu devomer si gnippam eht dna ,llac metsys ‘)(pamm’
eht gnisu deveihca si ecaps yromem dna elif a neewteb gnippam hcuS .)ecaps sserdda lautriv
nwo sti sah ssecorp hcaE :rednimeR( ecaps yromem lautriv eht fo tnemges a ot elif a gnippam
yb deveihca si sihT .yromem s’margorp eht fo noitrop a era yeht fi sa selif eldnah nac selif
deppam-yromem gnitroppus msinahcem A .selif deppam-yromem no sesucof tnemngissa siht
,gnikaeps yllareneG .secaps sserdda ssecorp otni selif pam ot dna sessecorp gnoma yromem
erahs ot desu era owt esehT .sllac metsys pmnum dna pamm eht gnitnemelpmi ni uoy ediug
ot mroftalp eht sa ,metsys gnitarepo gnihcaet ekil-xinU dna elpmis a ,6vx sesu tnemngissa sihT
noitcudortnI
3A-noitcurtsnI-0513CSC eht
4 (One directory and one pdf.)
2 proc.c proc.h sysfile.c trap.c
4 (three .c files and one .h file)
2 zip-q-rAssignment_3_120010001.zipAssignment_3_120010001
5 Assignment_3_120010001 Assignment_3_120010001.zip
.secalp nevig ni snoitcnuf tnemelpmi dluohs uoY .tsil deliated a si krow dluohs noitcnuf
/tnenopmoc siht woh fo cigol eht ,snoitces eseht nI .snoitces ODOT era )*( tuohtiw snoitceS
fdp.3ver-vcsir-koob/6vx/2202/828.6/ude.tim.liasc.sodp//:sptth .a
.sliated erom teg ot “koob-6vx” daer ot emoclew era uoy
,tituobaeromnraelottnawdnametsys6vxehtnidetseretnieraohwstnedutsroF)noitpo( .3
.tnemngissa siht gnildnah rof hguone eb dluow esoht
tahteveilebeW.tnemngissaruoygnitratserofebtnetnocehtfoaedicisabaevahuoyeruseB .2
.mehtesuotwohdnakrowsnoitcnufwohtuoerugifesaelp,snoitcesnoitcudortniehtroF .1
.strap ODOT
eht tpecxe meht EGNAHC TON oD .tnemngissa siht rof ti esu ot deen thgim uoY .stnenopmoc
eseht htiw skrow metsys eht woh dna tuoba si metsys siht tahw dnatsrednu uoy pleh lliw
taht snoitcnuf dna sloot ecudortni snoitces esehT .snoitces noitcudortni era )*( htiw snoitceS
.yrotcerid ‘resu/2202-sbal-6vx’ eht rednu c.tsetpamm ni noitcnuf
niam eht si margorp tset eht fo )gninrael trats yam uoy erehw( yrtne ehT .stnemmoc “:ODOT”
htiw gnitrats ,selif ruof nihtiw noitatnemelpmi ruoy timil eW
enilediuG noitcurtsnI
.edoc ruoy gnisserpmoc rof
pets elpmas eht si ereH .noitcuded edarg esuac dluow hctamsim tamroF .edoc ecruos ruoy htiw
rehtegot ,fdp fo tamrof eht ni dettimbus eb dluohs troper ehT .piz.100010021_3_tnemngissA
,elpmaxe rof ,evoba dna woleb nwohs edoc eht sa DI tneduts ruoy
gnisu ti eman dna elif piz elgnis a otni redlof toor erutcurts elif eht ni selif lla sserpmoc esaelP
c.part ,c.elifsys ,h.corp ,c.corp
1 void argint(int, int*);
2 int argstr(int, char*, int);
3 void argaddr(int, uint64 *);
4 int argfd(int n, int *pfd, struct file **pf);
1 // Defined in proc.h
2 struct proc {
3 struct spinlock lock;
5 // p->lock must be held when
6 enum procstate state;
7 void *chan;
8 int killed;
9 int xstate;
using these:
// Process state
// If non-zero, sleeping on chan
// If non-zero, have been killed
// Exit status to be returned to parent’s wait // Process ID
10 int pid; 11
12 // wait_lock must be held when using this:
13 struct proc *parent; // Parent process
15 // these are private to the process, so p->lock need not be held.
16 uint64 kstack;
17 uint64 sz;
18 pagetable_t pagetable;
19 struct trapframe *trapframe;
20 struct context context;
21 struct file *ofile[NOFILE];
22 struct inode *cwd;
23 char name[16];
24 struct VMA vma[VMASIZE];
// Virtual address of kernel stack // Size of process memory (bytes) // User page table
// data page for trampoline.S
// swtch() here to run process // Open files
// Current directory
// Process name (debugging) // virtual mem area
.)43:c.llacsys/lenrek( retsiger resu devas etairporppa
eht eveirter ot wargra llac lla yehT .rotpircsed elif ro ,retniop ,regetni na sa emarf part
eht morf tnemugra llac metsys ht’ n eht eveirter dfgra dna ,rddagra ,tnigra snoitcnuf lenrek ehT
3.4 retpahc >koob-6vx<
*gnihctef stnemugrA
.snoitces noitcudortni eht ni dedivorp sIPA dna cigol eht no desab
noitatnemelpmiehttuoerugifotdeenuoY.erehnwohseblliwedocelpmason,revewoH .1
27 // Defined in proc.c
28 // Return the current struct proc *, or zero if none.
29 struct proc* myproc(void)
1 // Defined in riscv.h
2 typedef uint64 pte_t;
3 typedef uint64 *pagetable_t; // 512 PTEs
5 #define PGSIZE 4096 // bytes per page
6 #define PGSHIFT 12 // bits of offset within a page
8 #define PGROUNDUP(sz) (((sz)+PGSIZE-1) & ~(PGSIZE-1))
9 #define PGROUNDDOWN(a) (((a)) & ~(PGSIZE-1))
11 #define PTE_V (1L << 0) // valid
12 #define PTE_R (1L << 1)
13 #define PTE_W (1L << 2)
14 #define PTE_X (1L << 3)
15 #define PTE_U (1L << 4) // user can access
18 // one beyond the highest possible virtual address.
19 // MAXVA is actually one bit less than the max allowed by
20 // Sv39, to avoid having to sign-extend virtual addresses
21 // that have the high bit set.
22 #defineMAXVA(1L<<(9+9+9+12-1))
1 // Defined in fcntl.h
2 #define PROT_NONE 0x0
3 #define PROT_READ 0x1
4 #define PROT_WRITE 0x2
5 #define PROT_EXEC 0x4
*sgalF & stroP
3 retpahc >koob-6vx<
7 #define MAP_SHARED 0x01
8 #define MAP_PRIVATE 0x02
1 void usertrap(void) 2{
6 7} 8 ... 9}
/// TODO: manage pagefault
else if(r_scause() == 13 || r_scause() == 15){
11 // Supervisor Trap Cause
12 static inline uint64
13 r_scause()
15 uint64 x;
16 asm volatile("csrr %0, scause" : "=r" (x) );
17 return x;
20 // Supervisor Trap Value
21 static inline uint64
22 r_stval()
24 uint64 x;
25 asm volatile("csrr %0, stval" : "=r" (x) );
26 return x;
.noitpecxe tluaFegaP eht eldnah ot deksa era uoy ,tnemngissa siht nI .edoc noitpecxe eht teg ot
)(esuacs_r sllac tI .ecaps resu morf llac metsys ro ,noitpecxe ,tpurretni na seldnah partresU
Programming Help
1 2 3 4 5 6 7 8 9
// read a page of file to address mem
// The off parameter in the mapfile and readi represents the offset // from the start of the file where the read operation should begin. void mapfile(struct file * f, char * mem, int offset){
printf(“off %d\n”, offset);
ilock(f->ip);
readi(f->ip, 0, (uint64) mem, offset, PGSIZE); iunlock(f->ip);
// Create PTEs for virtual addresses starting at va that refer to // physical addresses starting at pa. va and size might not
// be page-aligned. Returns 0 on success, -1 if walk() couldn’t
))(segappam dna )(elifpam( .amv
gnidnopserroc eht htiw elif eht fo egap eno pam ,yromem lacisyhp ni ecaps eraps si ereht fI
)snoitcnuf )(delliktes dna )(collak esu ot nrael llahs uoY( .ssecorp eht llik ot detcepxe era uoy
,dellif si yromem lacisyhp eht fI .metsys 6vx eht ni detroppus ton si msinahcem gnippaws ehT
)noitpecxe eht gnisuac sserdda eht .e.i( .eulav part sedivorp )(lavts_r
1 2 3 4 5 6 7 8 9
// allocate a needed page-table page.
int mappages(pagetable_t pagetable, uint64 va, uint64 size, uint64 pa, int perm) {
uint64 a, last; pte_t *pte;
if(size == 0) panic(“mappages: size”);
a = PGROUNDDOWN(va);
last = PGROUNDDOWN(va + size – 1); for(;;){
if((pte = walk(pagetable, a, 1)) == 0) return -1;
if(*pte & PTE_V) panic(“mappages: remap”);
*pte = PA2PTE(pa) | perm | PTE_V; if(a == last)
a += PGSIZE; pa += PGSIZE;
return 0; }
// Defined in file.h
struct file {
enum { FD_NONE, FD_PIPE, FD_INODE, FD_DEVICE } type; int ref; // reference count
char readable;
char writable;
struct pipe *pipe;
struct inode *ip;
short major;
// in-memory copy of
struct inode { uint dev; uint inum; int ref;
// Device number // Inode number // Reference count
// FD_PIPE
// FD_INODE and FD_DEVICE // FD_INODE
// FD_DEVICE
18 struct sleeplock lock; // protects everything below here
19 int valid; 20
21 short type;
22 short major;
23 short minor;
24 short nlink;
25 uint size;
26 uint addrs[NDIRECT+1];
// inode has been read from disk? // copy of disk inode
29 // Write to file f.
30 // addr is a user virtual address.
31 int filewrite(struct file *f, uint64 addr, int n);
33 // Increment ref count for file f.
34 struct file* filedup(struct file*);
36 // Close file f. (Decrement ref count, close when reaches 0.)
37 void fileclose(struct file*);
))(krof ,)(pamm( .elif gnissecca fo tnemercni na si ereht nehw dekovni eb lliw )(pudelif
.)(esolcelif ot ylralimiS .ssecorp fo tixe gnillaC
ro pamnum gnillaC .e.i .revo si pam yromem eht nehw kcab etirw ot dekovni eb lliw )(etirwelif
.noitamrofni ruoy rof detneserp si “edoni” “elif” tcurtS
1 // Defined in fs.c 2
3 // Read data from inode.
4 // Caller must hold ip->lock.
5 // If user_dst==1, then dst is a user virtual address;
6 // otherwise, dst is a kernel address.
7 int readi(struct inode *ip, int user_dst, uint64 dst, uint off, uint n);
9 // Write data to inode.
10 // Caller must hold ip->lock.
11 // If user_src==1, then src is a user virtual address;
12 // otherwise, src is a kernel address.
13 // Returns the number of bytes successfully written.
14 // If the return value is less than the requested n,
15 // there was an error of some kind.
16 int writei(struct inode *ip, int user_src, uint64 src, uint off, uint n);
18 // Lock the given inode.
19 // Reads the inode from disk if necessary.
20 void ilock(struct inode *ip);
22 // Unlock the given inode.
23 void iunlock(struct inode *ip);
1 // we already define size of VMA array for you
2 #define VMASIZE 16
4 // TODO: complete struct of VMA
5 struct VMA {
.ssecorp hcae rof deppam sah pamm tahw fo kcart peeK
noitatnemelpmI
.rof desu si tcurts amv
eht tahw si sihT .htiw detaicossa er’yeht ecived ro elif tahw dna ,evah yeht snoissimrep tahw
,era yeht egral woh ,trats yeht erehw gnidulcni ,sgnippam eseht fo kcart peek ot sdeen metsys
gnitarepo ehT .tcejbo fo dnik emas eht yb dekcab si dna snoissimrep emas eht sah taht yromem
lautriv fo noiger suougitnoc a stneserper AMV hcaE .ssecorp a fo ecaps sserdda eht otni deppam
era taht snoiger yromem eht kcart dna eganam ot desu si tcurts )aerA yromeM lautriV( AMV ehT
noitanalpxE
tcurtS AMV )ODOT(
.krow skcol dna ,elif ,edoni woh wonk ot )(nepo_sys ta kool a ekat yam uoY
.yromem eht fo ycnetsisnoc erusne ot desu era hcihw ,edoni fo skcol era )(kcolnui dna )(kcoli
)(ietirw sa ylralimiS
.sserdda dna sretniop
htiw laed hcihw ,)(ypcmem ro )(daer tuoba kniht ,gniod si )(idaer tahw aedi on evah uoy fI
.)(idaer ot dnes dluohs uoy retemarap eht tuo erugif
dna skrow )(idaer woh ot noitnetta yap ,tluaf egap gnildnah nehw esu ot deen uoy taht noitcnuF
1 // Defined in user.h
2 void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
4 // TODO: kernel mmap executed in sysfile.c
6 sys_mmap(void)
.ton ro sdeeccus pamm
rehtehw setacidni eulav nruter ehT .elif eht ni tesffo gnitrats eht si ‘tesffo’ tnemugra tsal ehT
.deppamebotelifehtfonoitpircsedelifehtsitnemugrahtfifehT.)esacsihtnidetibihorp
era setadpu ,DAER_TORP ot eud ,esruoc fo( elif emas eht gnippam sessecorp rehto
htiwderahsronelifehtotkcabnettirwebtondluohsnoitacifidomeht,yromemdeppam
eht seifidom ssecorp eht fi taht setacidni ‘ETAVIRP_PAM’ tnemugra htruof ehT .dewolla ton
sinoitacifidom,.e.i,ylno-daerebdluohsyromemdeppamehttahtsetacidni’DAER_TORP’
tnemugra driht ehT .pam ot setyb ynam woh setacidni ‘htgnel’ tnemugra dnoces
ehT .)orez eb syawla lliw ‘rdda’ taht emussa nac uoy ,krowemoh siht nI( sserdda lautriv
eht esoohc dluohs lenrek eht taht setacidni tnemugra ‘0’ tsrif ehT .ecaps sserdda eht otni
‘df’ elif fo tnetnoc eht pam ot lenrek eht sksa llac sihT .’;)0 ,df ,ETAVIRP_PAM ,DAER_TORP
,2*EZISGP ,0(pamm = p* rahc’ llac ew ,c.tsetpamm eht nI :noitanalpxe stnemugrA
)(pamm )ODOT(
.rewsna tcerroc eno ylno ton si erehT
.meht esu ot eerf leef ,noitatnemelpmi rehtruf rof AMV ni selbairav erom esu ot ekil dluow uoy fI
;deppam gnieb elif eht rof elif tcurts a ot retniop a niatnoc dluohs AMV ehT
.)(pammotnitneseblliwretemaraptahwtakoolaekaT
)uoy rof EZISAMV enifed ydaerla
I(.tneiciffusebdluohs61foezisA.dedeensayarratahtmorfetacolladnasAMVfoyarraezis
-dexif a eralced ot KO s’ti ,lenrek eht ni rotacolla yromem a evah t’nseod lenrek 6vx eht ecniS
.pamm yb detaerc egnar yromem lautriv a rof .cte ,elif ,snoissimrep ,htgnel
,sserdda eht gnidrocer ,)aera yromem lautriv( AMV eht ot gnidnopserroc erutcurts a enifeD
Code Help
tluaf egap fo elpmaS
)yltcerroc krow( detnemelpmi )(pamm retfa srucco tluaf egaP
detnemelpmi )(pamm erofeB
.tsetpamm llik dna tluaf egap a esuac lliw yromem de-pamm eht ot ssecca
tsrif eht tub ,deeccus dluohs pamm tsrif eht :detnemelpmi )(pamm retfa tsetpamm nuR
ees :tnih( desolc si elif eht nehw raeppasid t’nseod erutcurts eht taht os tnuoc ecnerefer
s’elif eht esaercni dluohs pamm ;deppam gnieb elif eht rof elif tcurts a ot retniop a niatnoc
dluohs AMV ehT .snoiger deppam fo elbat s’ssecorp eht ot AMV a dda dna ,elif eht pam
ot hcihw ni ecaps sserdda s’ssecorp eht ni noiger desunu na dniF :pamm fo noitatnemelpmI
.noitces gnihctef stnemugrA eht eeS .rotpircsed
elif a ro ,retniop ,regetni na sa emarf part eht morf tnemugra llac metsys ht’ n eht
eveirter dfgra dna ,rddagra ,tnigra snoitcnuf lenrek ehT .sllac metsys era yeht taht etacidni
ot dna snoitcnuf rehto morf meht hsiugnitsid ot _sys htiw dexiferp era sllac metsys ,metsys
gnitarepo 6vx eht nI .llac metsys )(xxx eht fo noitatnemelpmi s’lenrek eht si noitcnuf )(xxx_sys
trahC ssergorP
parT noitceS eeS
.pamnum tsrif eht ot teg dluohs ti ;tsetpamm nuR .egap eht no yltcerroc snoissimrep eht teS
.)idaer ot dessap edoni eht kcolnu/kcol ot evah lliw
uoy tub( elif eht ni daer ot hcihw ta tnemugra tesffo na sekat hcihw ,idaer htiw elif eht daeR
.ecaps sserdda resu eht otni ti pam dna ,egap taht otno elif tnaveler eht fo setyb 6904 daeR
.sserdda tluaf yb amv dilav gnidnopserroc dniF
.yromem lacisyhp fo egap a etacolla ot noiger de-pamm a ni tluaf-egap a esuac ot edoc ddA
6.4 ,5.4 retpahc >koob-6vx<
eldnaH tluaFegaP )ODOT(
Code Help, Add WeChat: cstutorcs
1 // TODO: complete munmap() 2 uint64
3 sys_munmap(void)
7 //defined in vm.c
8 void uvmunmap(pagetable_t pagetable, uint64 va, uint64 npages, int do_free);
.yrotcerid
'2202-sbal-6vx/~' rednu sdnammoc gniwollof eht gnisu edoc ruoy fo ssentcerroc eht tset nac uoY
'01 sunob + '09 trap margorP
seluR gnidarG
.krow noitatnemelpmi erom eriuqer dluow ti tub ,relooc
eb dluow rettal ehT .tnerap eht htiw egap a gnirahs fo daetsni egap lacisyhp wen a etacolla
ot KO si ti ,dlihc eht fo reldnah tluaf egap eht nI .elif tcurts s'AMV a rof tnuoc ecnerefer eht
tnemercni ot tegrof t'noD .tnerap eht sa snoiger deppam emas eht sah dlihc eht taht erusnE
eht htiw ssecorp bus a setaerc )(krof taht wonk ydaerla dluohs uoy ,1 tnemngissA ruoy nI
.dekovni si )(krof nehw skrow )(pamm woh eldnah dluohs uoy ,eroferehT .ofni emas
eldnaH kroF )SUNOB(
.stib D ta gnikool tuohtiw kcab segap gnitirw htiw yawa teg nac uoy ,suht
;kcab nettirw ton era segap ytrid-non taht kcehc ton seod tsetpamm ,revewoH .nettirw
neeb sah egap a rehtehw setacidni ETP V-CSIR eht ni )D( tib ytrid ehT .deifidom yllautca
margorp eht taht segap DERAHS_PAM kcab etirw ylno dluow noitatnemelpmi ruoy yllaedI
.noitaripsni rof etirwelif ta kooL .elif eht ot kcab egap
eht etirw ,DERAHS_PAM deppam si elif eht dna deifidom neeb sah egap deppamnu na fI
.elif tcurts gnidnopserroc eht fo
tnuoc ecnerefer eht esaerced dluohs ti ,pamm suoiverp a fo segap lla sevomer pamnum fI
.)pamnumvu esu :tnih( segap deificeps eht pamnu dna egnar sserdda eht rof AMV eht dnif
:pamnum tnemelpmI
)(pamnum )ODOT(
1 make qemu 2 mmaptest
1 $ mmaptest
2 mmap_test starting 3 test mmap f
5 off 4096
6 test mmap f: OK
7 test mmap private 8 off 0
9 off 4096
10 test mmap private: OK 11 test mmap read-only
12 test mmap read-only: OK 13 test mmap read/write 14 off 0
15 off 4096
16 test mmap read/write: OK
17 test mmap dirty
18 test mmap dirty: OK
19 test not-mapped unmap
20 test not-mapped unmap: OK
21 test mmap two files
24 test mmap two files: OK
25 mmap_test: ALL OK
26 fork_test starting
27 off 4096
31 off 4096
32 fork_test OK
33 mmaptest: all tests succeeded
stuptuo gniwollof eht evah ot detcepxe era uoY .smargorp ruoy tset ot tsetpamm elif elbatucexe
eht setucexe dnammoc 'tsetpamm' .'tsetpamm' gnidulcni selif eht ees ot dnammoc 'sl' etucexe
nac uoY .'$' htiw gnitrats lanimret ruoy ees lliw uoy dna ,metsys 6vx eht no snrut 'umeq ekam'
.tamrofFDPnitroperruoyeeslliwuoydnaelipmoceRkcilC .3
.edivorpewetalpmetehtdaolpudnamottobthgilhgihgniwollofehtkcilC .2
.tcejorpknalbwenaetaerC .1
.rotidE xeTaL enilno na ,faelrevO esu thgim uoy ,ecneinevnoc ruoy roF
rotidE xeTaL
eb ton lliw setalpmet rehto no desab stropeR .sliated gnidarg evitcepser dna strap tnatropmi
dezisahpme evah ew erehw ,troper eht rof etalpmet xetal dedivorp eht wollof yltcirts llahs uoY
'01 trap tropeR
)sunob( tset_krof
sseccuS elipmoC
selif owt pamm
ytrid pamm
etirw/daer pamm
ylno-daer pamm
etavirp pamm
pamnu deppam-ton