2019-08-05 06:04:44 +00:00
|
|
|
//
|
|
|
|
// test program for the alarm lab.
|
|
|
|
// you can modify this file for testing,
|
|
|
|
// but please make sure your kernel
|
|
|
|
// modifications pass the original
|
|
|
|
// versions of these tests.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "kernel/param.h"
|
|
|
|
#include "kernel/types.h"
|
|
|
|
#include "kernel/stat.h"
|
|
|
|
#include "kernel/riscv.h"
|
|
|
|
#include "user/user.h"
|
|
|
|
|
|
|
|
void test0();
|
|
|
|
void test1();
|
|
|
|
void periodic();
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
test0();
|
|
|
|
test1();
|
2019-10-03 19:02:19 +00:00
|
|
|
exit(0);
|
2019-08-05 06:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
volatile static int count;
|
|
|
|
|
|
|
|
void
|
|
|
|
periodic()
|
|
|
|
{
|
|
|
|
count = count + 1;
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("alarm!\n");
|
2019-08-05 06:04:44 +00:00
|
|
|
sigreturn();
|
|
|
|
}
|
|
|
|
|
|
|
|
// tests whether the kernel calls
|
|
|
|
// the alarm handler even a single time.
|
|
|
|
void
|
|
|
|
test0()
|
|
|
|
{
|
|
|
|
int i;
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("test0 start\n");
|
2019-08-05 06:04:44 +00:00
|
|
|
count = 0;
|
|
|
|
sigalarm(2, periodic);
|
|
|
|
for(i = 0; i < 1000*500000; i++){
|
2019-10-03 19:02:19 +00:00
|
|
|
if((i % 1000000) == 0)
|
2019-08-05 06:04:44 +00:00
|
|
|
write(2, ".", 1);
|
|
|
|
if(count > 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sigalarm(0, 0);
|
|
|
|
if(count > 0){
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("test0 passed\n");
|
2019-08-05 06:04:44 +00:00
|
|
|
} else {
|
2019-10-03 19:02:19 +00:00
|
|
|
printf("\ntest0 failed: the kernel never called the alarm handler\n");
|
2019-08-05 06:04:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void __attribute__ ((noinline)) foo(int i, int *j) {
|
|
|
|
if((i % 2500000) == 0) {
|
|
|
|
write(2, ".", 1);
|
|
|
|
}
|
|
|
|
*j += 1;
|
|
|
|
}
|
|
|
|
|
2019-10-03 19:02:19 +00:00
|
|
|
//
|
|
|
|
// tests that the kernel calls the handler multiple times.
|
|
|
|
//
|
|
|
|
// tests that, when the handler returns, it returns to
|
|
|
|
// the point in the program where the timer interrupt
|
|
|
|
// occurred, with all registers holding the same values they
|
|
|
|
// held when the interrupt occurred.
|
|
|
|
//
|
2019-08-05 06:04:44 +00:00
|
|
|
void
|
|
|
|
test1()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("test1 start\n");
|
2019-08-05 06:04:44 +00:00
|
|
|
count = 0;
|
|
|
|
j = 0;
|
|
|
|
sigalarm(2, periodic);
|
|
|
|
for(i = 0; i < 500000000; i++){
|
|
|
|
if(count >= 10)
|
|
|
|
break;
|
|
|
|
foo(i, &j);
|
|
|
|
}
|
2019-10-03 19:02:19 +00:00
|
|
|
if(count < 10){
|
|
|
|
printf("\ntest1 failed: too few calls to the handler\n");
|
|
|
|
exit(1);
|
|
|
|
} else if(i != j){
|
|
|
|
// the loop should have called foo() i times, and foo() should
|
|
|
|
// have incremented j once per call, so j should equal i.
|
|
|
|
// once possible source of errors is that the handler may
|
|
|
|
// return somewhere other than where the timer interrupt
|
|
|
|
// occurred; another is that that registers may not be
|
|
|
|
// restored correctly, causing i or j or the address ofj
|
|
|
|
// to get an incorrect value.
|
|
|
|
printf("\ntest1 failed: foo() executed fewer times than it was called\n");
|
|
|
|
exit(1);
|
2019-08-05 06:04:44 +00:00
|
|
|
} else {
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("test1 passed\n");
|
2019-08-05 06:04:44 +00:00
|
|
|
}
|
|
|
|
}
|