-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab6_testcase.c
1121 lines (1013 loc) · 36.2 KB
/
lab6_testcase.c
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <xinu.h>
int flag = 0;
int TEST;
/**
* set NFRAMES smaller to test page replacement policy
* Please update The NFRAMES macro include/paging.h to test page replacement!!
* */
#define OP_READ 0
#define OP_WRITE 1
int fifo_swap = 0;
int ourpolicy_swap = 0;
bsd_t store_id[8] = {0};
void out_of_seq_calls(pid32 parent, bsd_t store){
if(deallocate_bs(store) != SYSERR){
kprintf("%s : deallocate_bs should have failed but went through\r\n", __FUNCTION__);
send(parent, SYSERR);
receive();
}
kprintf("%s : Sending OK to parent\r\n");
send(parent, OK);
receive();
}
/* test1 */
void test1proc(pid32 parent){
send(parent, OK);
}
/* test2 */
void test2proc(pid32 parent){
int *ptr;
store_id[0] = allocate_bs(100);
if(store_id[0] == SYSERR){
kprintf("GRADING : Test 2-allocate Failed\r\n");
send( parent, SYSERR);
flag++;
}else{
kprintf("test2: allocate_bs() ok\r\n");
}
if(open_bs(store_id[0]) == SYSERR){
kprintf("GRADING : Test 2-open Failed\r\n");
send( parent, SYSERR);
flag++;
}else{
kprintf("test2: open_bs() ok\r\n");
}
//if(xmmap(5000, 100, MAP_PRIVATE, store_id[0])== SYSERR){
if(xmmap(5000, 100, MAP_SHARED, store_id[0])== SYSERR){
kprintf("GRADING : Test 2-xmmap Failed\r\n");
close_bs(store_id[0]);
deallocate_bs(store_id[0]);
send( parent, SYSERR);
flag++;
}else{
kprintf("test2: open_bs() ok\r\n");
}
ptr = (int *)(5000*4096);
kprintf("GRADING : Test 2 Accessing the memory : %d\r\n", *((int32 *) ptr));
*ptr = 1729;
kprintf("GRADING : Test 2 Accessing the memory after write : %d\r\n", *((int32 *) ptr));
xmunmap(5000);
/*if(xmunmap(5000) == SYSERR){
kprintf("GRADING : Test 2-xmunmap failed\r\n");
close_bs(store_id[0]);
deallocate_bs(store_id[0]);
send( parent, SYSERR);
flag++;
}*/
close_bs(store_id[0]);
/*if(close_bs(store_id[0]) == SYSERR){
kprintf("GRADING : Test 2-close Failed\r\n");
deallocate_bs(store_id[0]);
send( parent, SYSERR);
flag++;
}*/
if(deallocate_bs(store_id[0]) == SYSERR){
kprintf("GRADING : Test 2-deallocate Failed\r\n");
send( parent, SYSERR);
flag++;
}
store_id[0] = -1;
if( flag==0){
kprintf("GRADING : Test 2 Successful\r\n");
send( parent,OK );
}
}
/* test3 */
void return_memory_value( pid32 parent, uint32 addr){
/* FIXME: address not mapped by this process
* The result depends on the meaning of SHARE mode?
* */
kprintf("GRADING : Test 3: Sending memory value [%d] to parent\r\n", *((int32 *)(addr)));
int v = *((int32 *)(addr));
while(1);
//send(parent, *((int32 *)(addr)));
//receive();
}
/* test4 */
// make sure the address returned by vgetmem() is above 4096th page.
void vgetmem_test( pid32 parent ){
int nsize = 100;
char *p = vgetmem( 100 );
/* expect page fault here */
*p = 88;
send(parent, p );
}
/* test 5-1 */
void sharedmap(pid32 parent, int offset, int expected_value){
int pageno = 5000;
int flag=0;
if(open_bs(store_id[1]) == SYSERR){
kprintf("GRADING : Test 5-1-open Failed\r\n");
send(parent, SYSERR);
return;
}
if(xmmap(pageno, 100, MAP_SHARED, store_id[1])== SYSERR){
kprintf("GRADING : Test 5-2-xmmap Failed\r\n");
close_bs(store_id[1]);
send(parent, SYSERR);
return;
}
int v = *( int *) ( pageno *NBPG + offset );
if( v != expected_value ){
kprintf("GRADING : Test 5-shared map Failed. Did not get the expected value. Expect %d, got %d\r\n", expected_value, v);
flag++;
send(parent, SYSERR);
}
xmunmap(pageno);
/*if(xmunmap(pageno) == SYSERR){
kprintf("GRADING : Test 5-xmunmap failed\r\n");
close_bs(store_id[1]);
deallocate_bs(store_id[1]);
send(parent, SYSERR);
return;
}*/
close_bs(store_id[1]);
/*if(close_bs(store_id[1]) == SYSERR){
kprintf("GRADING : Test 5-close Failed\r\n");
deallocate_bs(store_id[1]);
send(parent, SYSERR);
return;
}*/
if( flag == 0 ){
kprintf("GRADING : Test 5 Successful\r\n");
send(parent, OK);
}
}
/* test5 */
void privatemap(pid32 parent, int old_value, int offset){
int pageno = 5000;
if(open_bs(store_id[1]) == SYSERR){
kprintf("GRADING : Test 5-open Failed\r\n");
send(parent, SYSERR);
return;
}
if(xmmap(pageno, 100, MAP_PRIVATE, store_id[1])== SYSERR){
kprintf("GRADING : Test 5-xmmap Failed\r\n");
close_bs(store_id[1]);
send(parent, SYSERR);
return;
}
int* ptr = (int *)(pageno*NBPG + offset);
int v = *ptr;
if( v != old_value ){
kprintf("GRADING : Test 5 read Failed. Read unexpected value. Expected %d, Got %d\r\n", old_value, v);
}
// read. This read should not be visible to the other process
*ptr = 520;
xmunmap(pageno);
/*if(xmunmap(pageno) == SYSERR){
kprintf("GRADING : Test 5-xmunmap failed\r\n");
close_bs(store_id[1]);
deallocate_bs(store_id[1]);
send(parent, SYSERR);
return;
}*/
close_bs(store_id[1]);
/*if(close_bs(store_id[1]) == SYSERR){
kprintf("GRADING : Test 5-close Failed\r\n");
deallocate_bs(store_id[1]);
send(parent, SYSERR);
return;
}*/
//deallocate_bs(store_id[1]);
kprintf("GRADING : Test 5 Successful\r\n");
send(parent, OK);
}
/* test8 */
void invalid_memory_access(void){
int *ptr = (int *)(5000*4096);
kprintf("%s : GRADING : Test 8 Trying to access invalid memory [%u] : %d\r\n", __FUNCTION__, ptr, *ptr);
while(1);
}
/* test9 */
void xmmap_and_wait_for_kill(pid32 parent){
bsd_t store ;
store = allocate_bs(10);
if(store == SYSERR){
kprintf("%s : GRADING : Test 9 Allocate failed\r\n", __FUNCTION__);
send(parent, SYSERR);
receive();
}
if(open_bs(store) == SYSERR){
kprintf("%s : GRADING : Test 9 Open failed\r\n", __FUNCTION__);
send(parent, SYSERR);
receive();
}
if(xmmap(5000, 10, MAP_SHARED, store) == SYSERR){
kprintf("%s : GRADING : Test 9 Xmmap failed\r\n", __FUNCTION__);
send(parent, SYSERR);
receive();
}
kprintf("%s : GRADING : Test 9 Sending store id [%d] to parent\r\n", __FUNCTION__, store);
send(parent, store);
recvclr();
receive();
}
/* test10 */
void policytest(pid32 parent){
bsd_t local_store;
int i;
int* ptr;
kprintf("Running Test 10 with NFRAMES : %d\r\n", NFRAMES);
local_store = allocate_bs(100);
if(local_store == SYSERR){
kprintf("GRADING : Test 10 : could not allocate backing store\r\n");
send(parent, SYSERR);
return;
}
if(open_bs(local_store) == SYSERR){
kprintf("GRADING : Test 10 : could not open backing store\r\n");
send(parent, SYSERR);
return;
}
if(xmmap(5000, 100, MAP_SHARED, local_store) == SYSERR){
kprintf("GRADING : Test 10 : could not xmmap backing store\r\n");
send(parent, SYSERR);
return;
}
for(i=0; i< 95; i++){
kprintf("\rIteration [%3d]", i);
ptr = (int32 *)((5000+i)*4096+8);
*ptr = 10;
}
kprintf("\r\n");
xmunmap(5000);
/*if(xmunmap(5000) == SYSERR){
kprintf("GRADING : Test 10 : could not xmunmap backing store\r\n");
send(parent, SYSERR);
return;
}*/
if(close_bs(local_store) == SYSERR){
kprintf("GRADING : Test 10 : could not close backing store\r\n");
send(parent, SYSERR);
return;
}
if(deallocate_bs(local_store) == SYSERR){
kprintf("GRADING : Test 10 : could not deallocate backing store\r\n");
send(parent, SYSERR);
return;
}
kprintf("GRADING : Test 10 : Successful\r\n");
send(parent, OK);
}
void bonustest_read(pid32 parent, bsd_t store){
int pageno = 6000;
int pagerange = 200;
bsd_t local_store;
int count = 0;
/*while(count < 8 ){
local_store = allocate_bs(pagerange);
kprintf("GRADING : Bonus Test - allocate_bs gets %d\r\n", local_store);
if( local_store == store ){
kprintf("GRADING : Bonus Test - find the same backing store \r\n");
break;
}else if( local_store == SYSERR ){
kprintf("GRADING : Bonus Test -allocate_bs Failed\r\n");
//send(parent, SYSERR);
return;
}
//deallocate_bs( local_store );
count++;
}
for(count=0;count<8;count++){
if( count != store )
deallocate_bs( count );
}*/
//local_store = store;
local_store = allocate_bs(pagerange);
if(open_bs(local_store) == SYSERR){
kprintf("GRADING : Bonus Test -open Failed\r\n");
send(parent, SYSERR);
return;
}
kprintf("GRADING : Bonus Test - before xmmap()\r\n");
if(xmmap(pageno, 200, MAP_PRIVATE, local_store)== SYSERR){
kprintf("GRADING : Bonus Test-xmmap Failed\r\n");
close_bs(local_store);
send(parent, SYSERR);
return;
}
kprintf("GRADING : Bonus Test - after xmmap(). press enter\r\n");
char cmd[16];
read(CONSOLE, cmd, sizeof(cmd));
int* ptr;
int pg;
for(pg= pageno; pg<pageno+5;pg++){
ptr = (int *)(pageno*4096);
// perform read operation
int v = *ptr;
}
if(xmunmap(pageno) == SYSERR){
kprintf("GRADING : Bonus Test -xmunmap failed\r\n");
close_bs(local_store);
send(parent, SYSERR);
return;
}
if(close_bs(local_store) == SYSERR){
kprintf("GRADING : Bonus Test -close Failed\r\n");
send(parent, SYSERR);
return;
}
//deallocate_bs( local_store );
kprintf("GRADING : Bonus Test - read only finished\r\n");
send(parent, OK);
}
void bonustest_write(pid32 parent, bsd_t store){
int pageno = 5000;
unsigned int pagerange = 10;
bsd_t local_store = store;
while( 1 ){
local_store = allocate_bs(pagerange);
if( local_store == store ){
break;
}else if( local_store == SYSERR ){
kprintf("GRADING : Bonus Test -allocate_bs Failed\r\n");
send(parent, SYSERR);
return;
}
deallocate_bs( local_store );
}
if(open_bs(local_store) == SYSERR){
kprintf("GRADING : Bonus Test 2-open Failed\r\n");
send(parent, SYSERR);
return;
}
if(xmmap(pageno, 10, MAP_SHARED, local_store)== SYSERR){
kprintf("GRADING : Bonus Test 2-xmmap Failed\r\n");
close_bs(store_id[1]);
deallocate_bs(store_id[1]);
send(parent, SYSERR);
return;
}
char *ptr;
unsigned int pg,offset;
for(pg=pageno; pg< pageno+pagerange; pg++){
for(offset=0;offset < NBPG; offset++){
ptr = (char *)(pg*NBPG+offset);
*ptr = 1;
}
}
if(xmunmap(pageno) == SYSERR){
kprintf("GRADING : Bonus Test 2-xmunmap failed\r\n");
close_bs(store_id[1]);
deallocate_bs(store_id[1]);
send(parent, SYSERR);
return;
}
if(close_bs(local_store) == SYSERR){
kprintf("GRADING : Bonus Test 2-close Failed\r\n");
deallocate_bs(store_id[1]);
send(parent, SYSERR);
return;
}
if(open_bs(local_store) == SYSERR){
kprintf("GRADING : Bonus Test -open Failed\r\n");
send(parent, SYSERR);
return;
}
if(xmmap(pageno, 100, MAP_PRIVATE, local_store)== SYSERR){
kprintf("GRADING : Bonus Test-xmmap Failed\r\n");
close_bs(local_store);
send(parent, SYSERR);
return;
}
ptr = (int *)(pageno*4096);
*ptr = 20;
if(xmunmap(pageno) == SYSERR){
kprintf("GRADING : Bonus Test -xmunmap failed\r\n");
close_bs(local_store);
send(parent, SYSERR);
return;
}
if(close_bs(local_store) == SYSERR){
kprintf("GRADING : Bonus Test -close Failed\r\n");
send(parent, SYSERR);
return;
}
/*deallocate_bs( local_store ); */
send(parent, OK);
}
int main(int argc, char **argv){
bsd_t local_store;
pid32 pid;
int i=0;
int j=0;
int *ptr;
umsg32 message;
psinit();
kprintf("GRADING: Start\r\n");
//goto bonus;
/* Test case 01~03 are sanity check. */
TEST=0;
test01:
for(i=0; i< 8; i++){
allocate_bs(10);
}
if(allocate_bs(10) == SYSERR){
kprintf("GRADING : Initialization Test 1 Successful\r\n");
}
else{
kprintf("GRADING : Initialization Test 1 Failed\r\n");
}
for(i=0; i< 8; i++){
deallocate_bs(i);
store_id[i] = -1;
}
test02:
/* Test case 0-2: score: 0 */
flag = 0;
if(open_bs(6) != SYSERR){
kprintf("GRADING : Initialization Test 2-1 Failed\r\n");
flag++;
}
if(close_bs(1) != SYSERR){
kprintf("GRADING : Initialization Test 2-2 Failed\r\n");
flag++;
}
if(deallocate_bs(4) != SYSERR){
kprintf("GRADING : Initialization Test 2-3 Failed\r\n");
flag++;
}
if(flag == 0){
kprintf("GRADING : Initialization Test 2 Successful\r\n");
}
test03:
/*
* Test case 3 : score: 0
* Make sure I can only call allocate/open/close/deallocate on a backing store
* ie. Make sure all the calls are allowed only if they happen in a single process
*/
store_id[0] = allocate_bs(10);
recvclr();
pid = create(out_of_seq_calls, 2048, 30, "Out of Sequence Calls", 2, currpid, store_id[0]);
resume(pid);
if(receive() == OK){
kprintf("GRADING : Initialization Test 3 Succesful\r\n");
}
else{
kprintf("GRADING : Initialization Test 3 Failed\r\n");
}
open_bs(store_id[0]);
close_bs(store_id[0]);
deallocate_bs(store_id[0]);
//----------------------------------------------------------------------------------
// start of lab4 test cases
TEST=1;
test1:
/* Lab4
* Test case 1
*
* vcreate()
* */
recvclr();
pid = vcreate(test1proc, 2048,30, 30, "test1", 1, currpid);
resume(pid);
if(receive() == OK){
kprintf("GRADING : Test 1 Successful\r\n");
}
else{
kprintf("GRADING : Test 1 Failed\r\n");
}
TEST=2;
test2:
/*
* Test case 2 :
* Make sure you can access mapped address/write in vcreate'd processes
*
* This was adapted from test case 5, lab3
*/
flag = 0;
recvclr();
pid = vcreate(test2proc, 2048,30, 30, "test2", 1, currpid);
resume(pid);
if(receive() == OK){
kprintf("GRADING : Test 2 Successful\r\n");
}
else{
kprintf("GRADING : Test 2 Failed\r\n");
}
flag = 0;
goto test4;
test4:
TEST=4;
/* Lab4
* Test case 4
* test vgetmem() - required
* */
resume( vcreate(vgetmem_test, 2048, 30, 30, "Vgetmem test", 1, currpid) );
umsg32 m = receive();
kprintf("GRADING : Test 4 returned %d\r\n", m);
if( m >= 4096*4096 ){
kprintf("GRADING : Test 4 Successful\r\n");
}else{
kprintf("GRADING : Test 4 Failed\r\n");
}
test51:
TEST=51;
/*
* Test case 5-1
* test MAP_SHARED
*
* */
flag = 0;
store_id[1] = allocate_bs(100);
open_bs(store_id[1]);
int start = 6000;
xmmap(start, 100, MAP_SHARED, store_id[1]);
int offset = 100;
ptr = (int*)( start* NBPG + offset );
*ptr = 1024;
resume( vcreate(sharedmap,2048, 30, 30, "test5 sharedmap process", 3, currpid, offset, *ptr) );
int result = receive();
if( result == OK ){
kprintf("GRADING : Test 5-1 Successful.\r\n");
}
xmunmap(6000);
close_bs(store_id[1]);
deallocate_bs(store_id[1]);
test5:
TEST=5;
/* Lab4
* Test case 5
* Make sure PRIVATE xmmap mode works for new processes.
*
* */
// how to test? first, do read-only (map a large number of pages) copy and measure the time is ~ 0
// and then do a write after copy to make sure it does not fail
int expected_value = 100;
flag = 0;
store_id[1] = allocate_bs(100);
open_bs(store_id[1]);
xmmap(6000, 100, MAP_SHARED, store_id[1]);
ptr = (int*)( 6000*NBPG + offset );
*ptr = expected_value;
xmunmap(6000);
close_bs(store_id[1]);
// map again at different address
open_bs(store_id[1]);
xmmap(7000, 100, MAP_PRIVATE, store_id[1]);
// read it. should get the same value written before.
ptr = (int*)( 7000*NBPG + offset );
int v5 = *ptr;
if( v5 != expected_value ){
kprintf("GRADING : Test 5-performed read didn't get the expected value.\r\n");
flag++;
}
// another process writes the same backing store
resume( vcreate(privatemap,2048, 30, 30, "test5 privatemap process", 3, currpid, expected_value, offset) );
if( receive() != OK ){
flag++;
}
v5 = *ptr;
if( v5 != expected_value ){
kprintf("GRADING : Failed Test 5-value was changed by another process! .\r\n");
flag++;
}
xmunmap( 7000 );
close_bs(store_id[1]);
deallocate_bs(store_id[1]);
if( flag == 0 ){
kprintf("GRADING : Test 5 Successful.\r\n");
}
test6:
TEST=6;
/* Lab4
* Test case 6
* Make sure every process has its own page directory mapping
* -- repeat test case 3, but with create instead of vcreate
* */
flag = 0;
store_id[1] = allocate_bs(100);
if(store_id[1] == SYSERR){
kprintf("GRADING : Test 6-allocate Failed\r\n");
goto test7;
}
if(open_bs(store_id[1]) == SYSERR){
kprintf("GRADING : Test 6-open Failed\r\n");
goto test7;
}
//if(xmmap(5000, 100, MAP_SHARED, store_id[1])== SYSERR){
if(xmmap(5000, 100, MAP_SHARED, store_id[1])== SYSERR){
kprintf("GRADING : Test 6-xmmap Failed\r\n");
close_bs(store_id[1]);
deallocate_bs(store_id[1]);
goto test7;
}
ptr = (int *)(5020*4096);
kprintf("GRADING : Test 6 Accessing the memory : %d\r\n", *((int32 *) ptr));
*ptr = 1729;
kprintf("GRADING : Spawning another process to access the memory\r\n");
recvclr();
pid = create(return_memory_value, 2048, 30, "Return Memory Value", 2, currpid, 5020*4096);
resume(pid);
sleep(1);
if( kill(pid) == OK ){
kprintf("GRADING : Test 6 Failed because it should've cause invalid access.\r\n");
flag++;
}else{
kprintf("GRADING : Test 6 process killed as expected.\r\n");
}
kprintf("GRADING : Test 6 Accessing the memory after write : %d\r\n", *((int32 *) ptr));
xmunmap(5000);
/*if(xmunmap(5000) == SYSERR){
kprintf("GRADING : Test 6-xmunmap failed\r\n");
close_bs(store_id[1]);
deallocate_bs(store_id[1]);
goto test7;
}*/
if(close_bs(store_id[1]) == SYSERR){
kprintf("GRADING : Test 6-close Failed\r\n");
deallocate_bs(store_id[1]);
goto test7;
}
if(deallocate_bs(store_id[1]) == SYSERR){
kprintf("GRADING : Test 6-deallocate Failed\r\n");
goto test7;
}
if(flag == 0){
kprintf("GRADING : Test 6 Successful\r\n");
}
goto test7;
test7:
TEST=7;
/* Lab4
* Test case 7 :
* Make sure xmmap/write/xunmap/xmmap/read works
* and can be remapped by another vcreate process.
*/
flag = 0;
store_id[2] = -1;
store_id[3] = -1;
store_id[2] = allocate_bs(100);
if(store_id[2] == SYSERR){
kprintf("GRADING : Test 7-allocate Failed\r\n");
goto test3;
}
if(open_bs(store_id[2]) == SYSERR){
kprintf("GRADING : Test 7-open Failed\r\n");
goto test3;
}
if(xmmap(6000, 100, MAP_SHARED, store_id[2])== SYSERR){
kprintf("GRADING : Test 7-xmmap Failed\r\n");
close_bs(store_id[2]);
deallocate_bs(store_id[2]);
goto test3;
}
ptr = (int *)(6080*4096);
kprintf("GRADING : Test 7 Accessing the memory : %d\r\n", *((int32 *) ptr));
*ptr = 17290;
kprintf("GRADING : Test 7 Accessing the memory after write : %d\r\n", *((int32 *) ptr));
xmunmap(6000);
/*if(xmunmap(6000) == SYSERR){
kprintf("GRADING : Test 7-xmunmap failed\r\n");
close_bs(store_id[2]);
deallocate_bs(store_id[2]);
goto test3;
}*/
if(close_bs(store_id[2]) == SYSERR){
kprintf("GRADING : Test 7-close Failed\r\n");
deallocate_bs(store_id[2]);
goto test3;
}
kprintf("GRADING : Mapping and trying to read again\r\n");
if(open_bs(store_id[2]) == SYSERR){
kprintf("GRADING : Test 7-againopen Failed\r\n");
goto test3;
}
if(xmmap(6000, 100, MAP_SHARED, store_id[2])== SYSERR){
kprintf("GRADING : Test 7-againxmmap Failed\r\n");
close_bs(store_id[2]);
deallocate_bs(store_id[2]);
goto test3;
}
ptr =(int *)( 6080*4096);
kprintf("GRADING : Test 7 Accessing the memory should be 17290: %d\r\n", *((int32 *) ptr));
if(*((int32 *)(ptr)) != 17290){
kprintf("GRADING : Test 7 failed, memory value not persistent\r\n");
flag++;
}
xmunmap(6000);
/*if(xmunmap(6000) == SYSERR){
kprintf("GRADING : Test 7-againxmunmap failed\r\n");
close_bs(store_id[2]);
deallocate_bs(store_id[2]);
goto test3;
}*/
if(close_bs(store_id[2]) == SYSERR){
kprintf("GRADING : Test 7-againclose Failed\r\n");
deallocate_bs(store_id[2]);
goto test3;
}
if(deallocate_bs(store_id[2]) == SYSERR){
kprintf("GRADING : Test 7-deallocate Failed\r\n");
goto test3;
}
if(flag == 0){
kprintf("GRADING : Test 7 Successful\r\n");
}
goto test3;
test3:
TEST=3;
/*
* Test case 3 :
* Make sure another process(vcreate) can not read the memory I have written
* ie. Check all tables are not global
*
* This was adapted from testcase 6, lab 3
*/
flag = 0;
store_id[2] = allocate_bs(100);
if(store_id[2] == SYSERR){
kprintf("GRADING : Test 3-allocate Failed\r\n");
goto test10;
}
if(open_bs(store_id[2]) == SYSERR){
kprintf("GRADING : Test 3-open Failed\r\n");
goto test10;
}
//if(xmmap(5000, 100, MAP_SHARED, store_id[2])== SYSERR){
if(xmmap(5000, 100, MAP_SHARED, store_id[2])== SYSERR){
kprintf("GRADING : Test 3-xmmap Failed\r\n");
close_bs(store_id[2]);
deallocate_bs(store_id[2]);
goto test10;
}
ptr = (int *)(5020*4096);
kprintf("GRADING : Test 3 Accessing the memory : %d\r\n", *((int32 *) ptr));
*ptr = 1729;
kprintf("GRADING : Spawning another process to access the memory\r\n");
recvclr();
pid = vcreate(return_memory_value, 2048, 10, 30, "Return Memory Value", 2, currpid, 5020*4096);
resume(pid);
//sleep(1);
if( kill(pid) == OK ){
kprintf("GRADING : Test 3 Failed because it should've cause invalid access.\r\n");
flag++;
}else{
kprintf("GRADING : Test 3 process killed as expected.\r\n");
}
kprintf("GRADING : Test 3 Accessing the memory after write : %d\r\n", *((int32 *) ptr));
xmunmap(5000);
/*if(xmunmap(5000) == SYSERR){
kprintf("GRADING : Test 3-xmunmap failed\r\n");
close_bs(store_id[2]);
deallocate_bs(store_id[2]);
goto test10;
}*/
if(close_bs(store_id[2]) == SYSERR){
kprintf("GRADING : Test 3-close Failed\r\n");
deallocate_bs(store_id[2]);
goto test10;
}
if(deallocate_bs(store_id[2]) == SYSERR){
kprintf("GRADING : Test 3-deallocate Failed\r\n");
goto test10;
}
if(flag == 0){
kprintf("GRADING : Test 3 Successful\r\n");
}
goto test10;
test10:
TEST=10;
recvclr();
sleep(1); // sleep one second before test10 to reduce interference.
/* Lab4
* stress test vgetmem or vcreate?
* */
srpolicy( FIFO );
pid = vcreate(policytest, 2048, 10, 30, "stress test. policytest", 1, currpid);
resume(pid);
receive();
if( fifo_swap == 0 ){
kprintf("GRADING : Test 10 Failed. number of page-outs is zero! Either you did not call hook_pswap_out() or number of frames not dependent on NFRAMES\r\n" );
}else{
kprintf("GRADING : Test 10 Successful. number of page-outs is %d\r\n", fifo_swap );
}
test11:
TEST=11;
recvclr();
sleep(1); // sleep one second before test10 to reduce interference.
/* Lab4
* stress test vgetmem or vcreate?
* */
srpolicy( OURPOLICY );
pid = vcreate(policytest, 2048, 10, 30, "stress test. policytest", 1, currpid);
resume(pid);
receive();
if( ourpolicy_swap == 0 ){
kprintf("GRADING : Test 11 Failed. number of page-outs is zero! Either you did not call hook_pswap_out() or number of frames not dependent on NFRAMES\r\n" );
}else{
kprintf("GRADING : Test 11 Successful. number of page-outs is %d\r\n", ourpolicy_swap );
}
srpolicy( FIFO );
test8:
TEST=8;
/* Lab4
* Test 8
* Make sure invalid memory access kills() the process
* vcreate()
*/
recvclr();
pid = vcreate(invalid_memory_access, 2048, 10, 40, "Invalid Memory Access", 0);
resume(pid);
receive();
if(kill(pid) != SYSERR){
kprintf("GRADING : Test 8 Failed The process [%d] should have been already killed\r\n");
}
else{
kprintf("GRADING : Test 8 Successful\r\n");
}
test9:
TEST=9;
/* Lab4
* Test case 9
* Make sure kill() cleans up mappings and deallocates
* vcreate()
*/
recvclr();
pid = vcreate(xmmap_and_wait_for_kill, 2048, 10, 30, "XMMAP and wait for kill", 1, currpid);
resume(pid);
local_store= receive();
if(local_store == SYSERR){
kprintf("GRADING : Test 9 Failed the process returned SYSERR\r\n");
goto test10;
}
kill(pid);
i=0;
for(i=0; i< 8; i++){
store_id[i] = allocate_bs(10);
if(store_id[i] == SYSERR){
kprintf("GRADING : Test 9 allocation failed before 8 iterations i=[%d]\r\n", i);
break;
}
printf("GRADING : Test 9 :The store ID is %d\r\n", store_id[i]);
if(store_id[i] == local_store){
break;
}
}
if(i < 8 && store_id[i] == local_store ){
kprintf("GRADING : Test 9 Successful , killing the process deallocated the backing store\r\n");
for(j=0; j < 8; j++){
open_bs(store_id[j]);
close_bs(store_id[j]);
deallocate_bs(store_id[j]);
}
}
else{
kprintf("GRADING : Test 9 Failed, we could not allocate the store again in 8 iterations\r\n");
for(j=0; j < 8; j++){
open_bs(store_id[j]);
close_bs(store_id[j]);
deallocate_bs(store_id[j]);
}
}
bonus:
//#ifdef BONUS
{
int pageno = 5000;
int pagerange = 200;
char cmd[16];
kprintf("GRADING : bonus points section\r\n");
/* Lab4
* bonus points: 50 points capped at 280
* Make sure PRIVATE xmmap mode works for new processes. i.e., copy on write does work
*
* */
flag = 0;
int accessrange = 10;
// how to test? first, do read-only (map a large number of pages) copy and measure the time is ~ 0
// and then do a write after copy to make sure it does not fail
/*store_id[1] = allocate_bs(pagerange);
if(open_bs(store_id[1]) == SYSERR){
kprintf("GRADING : Bonus Test (prep)-open Failed\r\n");
deallocate_bs( store_id[1] );
goto done;
}
kprintf("GRADING : Bonus Test (prep) store id = %d\r\n", store_id[1]);
if(xmmap(pageno, pagerange, MAP_SHARED, store_id[1])== SYSERR){
kprintf("GRADING : Bonus Test (prep)-xmmap Failed\r\n");
close_bs(store_id[1]);
deallocate_bs( store_id[1] );
}
// make sure the pages are resident
int pg;
int offset;
//for(pg=pageno; pg< pageno+pagerange-1; pg++){
for(pg=pageno; pg< pageno+accessrange; pg++){
kprintf("GRADING : Writing to page %d.\r\n", pg);
offset=0;
//for(offset=0;offset < NBPG; offset++){
//for(offset=0;offset < 100; offset++){
ptr = (char *)(pg*NBPG+offset);
*ptr = (pg*NBPG+offset)%100 ;