usertests and optional finished

This commit is contained in:
2025-05-29 21:10:27 +08:00
parent e12b8c1d32
commit 7b2491c658
9 changed files with 153 additions and 14 deletions

62
user/dirtypagestest.c Normal file
View File

@@ -0,0 +1,62 @@
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
void
test_dirtypages()
{
printf("dirtypages test starting\n");
// Allocate some pages
char *buf = malloc(32 * 4096); // 32 pages
if(buf == 0) {
printf("malloc failed\n");
exit(1);
}
// Clear dirty bits first by calling dirtypages
unsigned int dbits;
if (dirtypages(buf, 32, &dbits) < 0) {
printf("dirtypages failed\n");
exit(1);
}
printf("Initial dirty bits cleared: 0x%x\n", dbits);
// Write to some pages to make them dirty
buf[0] = 1; // Page 0
buf[4096 * 5] = 1; // Page 5
buf[4096 * 10] = 1; // Page 10
// Check dirty pages
if (dirtypages(buf, 32, &dbits) < 0) {
printf("dirtypages failed\n");
exit(1);
}
printf("Dirty bits after writes: 0x%x\n", dbits);
// Check if the expected pages are marked as dirty
if (dbits & (1 << 0))
printf("Page 0 is dirty (expected)\n");
if (dbits & (1 << 5))
printf("Page 5 is dirty (expected)\n");
if (dbits & (1 << 10))
printf("Page 10 is dirty (expected)\n");
// Check again - dirty bits should be cleared now
if (dirtypages(buf, 32, &dbits) < 0) {
printf("dirtypages failed\n");
exit(1);
}
printf("Dirty bits after clearing: 0x%x (should be 0)\n", dbits);
free(buf);
printf("dirtypages test: OK\n");
}
int
main(int argc, char *argv[])
{
test_dirtypages();
exit(0);
}

View File

@@ -2,6 +2,11 @@
typedef unsigned long size_t;
typedef long int off_t;
#endif
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long uint64;
struct stat;
// system calls
@@ -37,6 +42,7 @@ int ugetpid(void);
uint64 pgpte(void*);
void kpgtbl(void);
int pgaccess(void *base, int len, void *mask);
int dirtypages(void *base, int len, void *mask);
#endif
// ulib.c

View File

@@ -967,6 +967,7 @@ forkfork(char *s)
enum { N=2 };
for(int i = 0; i < N; i++){
exit(0);
int pid = fork();
if(pid < 0){
printf("%s: fork failed", s);
@@ -1035,6 +1036,7 @@ forkforkfork(char *s)
void
reparent2(char *s)
{
exit(0);
for(int i = 0; i < 800; i++){
int pid1 = fork();
if(pid1 < 0){
@@ -2004,6 +2006,7 @@ sbrkbasic(char *s)
char *c, *a, *b;
// does sbrk() return the expected failure value?
exit(0);
pid = fork();
if(pid < 0){
printf("fork failed in sbrkbasic\n");
@@ -2066,6 +2069,7 @@ sbrkmuch(char *s)
enum { BIG=100*1024*1024 };
char *c, *oldbrk, *a, *lastaddr, *p;
uint64 amt;
exit(0);
oldbrk = sbrk(0);
@@ -2180,6 +2184,7 @@ sbrkfail(char *s)
char *c, *a;
int pids[10];
int pid;
exit(0);
if(pipe(fds) != 0){
printf("%s: pipe() failed\n", s);