《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

639 regels
23 KiB

12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
  1. #!/bin/sh
  2. verbose=false
  3. if [ "x$1" = "x-v" ]; then
  4. verbose=true
  5. out=/dev/stdout
  6. err=/dev/stderr
  7. else
  8. out=/dev/null
  9. err=/dev/null
  10. fi
  11. ## make & makeopts
  12. if gmake --version > /dev/null 2>&1; then
  13. make=gmake;
  14. else
  15. make=make;
  16. fi
  17. makeopts="--quiet --no-print-directory -j"
  18. make_print() {
  19. echo `$make $makeopts print-$1`
  20. }
  21. ## command tools
  22. awk='awk'
  23. bc='bc'
  24. date='date'
  25. grep='grep'
  26. rm='rm -f'
  27. sed='sed'
  28. ## symbol table
  29. sym_table='obj/kernel.sym'
  30. ## gdb & gdbopts
  31. gdb="$(make_print GDB)"
  32. gdbport='1234'
  33. gdb_in="$(make_print GRADE_GDB_IN)"
  34. ## qemu & qemuopts
  35. qemu="$(make_print qemu)"
  36. qemu_out="$(make_print GRADE_QEMU_OUT)"
  37. if $qemu -nographic -help | grep -q '^-gdb'; then
  38. qemugdb="-gdb tcp::$gdbport"
  39. else
  40. qemugdb="-s -p $gdbport"
  41. fi
  42. ## default variables
  43. default_timeout=30
  44. default_pts=5
  45. pts=5
  46. part=0
  47. part_pos=0
  48. total=0
  49. total_pos=0
  50. ## default functions
  51. update_score() {
  52. total=`expr $total + $part`
  53. total_pos=`expr $total_pos + $part_pos`
  54. part=0
  55. part_pos=0
  56. }
  57. get_time() {
  58. echo `$date +%s.%N 2> /dev/null`
  59. }
  60. show_part() {
  61. echo "Part $1 Score: $part/$part_pos"
  62. echo
  63. update_score
  64. }
  65. show_final() {
  66. update_score
  67. echo "Total Score: $total/$total_pos"
  68. if [ $total -lt $total_pos ]; then
  69. exit 1
  70. fi
  71. }
  72. show_time() {
  73. t1=$(get_time)
  74. time=`echo "scale=1; ($t1-$t0)/1" | $sed 's/.N/.0/g' | $bc 2> /dev/null`
  75. echo "(${time}s)"
  76. }
  77. show_build_tag() {
  78. echo "$1:" | $awk '{printf "%-24s ", $0}'
  79. }
  80. show_check_tag() {
  81. echo "$1:" | $awk '{printf " -%-40s ", $0}'
  82. }
  83. show_msg() {
  84. echo $1
  85. shift
  86. if [ $# -gt 0 ]; then
  87. echo "$@" | awk '{printf " %s\n", $0}'
  88. echo
  89. fi
  90. }
  91. pass() {
  92. show_msg OK "$@"
  93. part=`expr $part + $pts`
  94. part_pos=`expr $part_pos + $pts`
  95. }
  96. fail() {
  97. show_msg WRONG "$@"
  98. part_pos=`expr $part_pos + $pts`
  99. }
  100. run_qemu() {
  101. # Run qemu with serial output redirected to $qemu_out. If $brkfun is non-empty,
  102. # wait until $brkfun is reached or $timeout expires, then kill QEMU
  103. qemuextra=
  104. if [ "$brkfun" ]; then
  105. qemuextra="-S $qemugdb"
  106. fi
  107. if [ -z "$timeout" ] || [ $timeout -le 0 ]; then
  108. timeout=$default_timeout;
  109. fi
  110. t0=$(get_time)
  111. (
  112. ulimit -t $timeout
  113. exec $qemu -nographic $qemuopts -serial file:$qemu_out -monitor null -no-reboot $qemuextra
  114. ) > $out 2> $err &
  115. pid=$!
  116. # wait for QEMU to start
  117. sleep 1
  118. if [ -n "$brkfun" ]; then
  119. # find the address of the kernel $brkfun function
  120. brkaddr=`$grep " $brkfun\$" $sym_table | $sed -e's/ .*$//g'`
  121. (
  122. echo "target remote localhost:$gdbport"
  123. echo "break *0x$brkaddr"
  124. echo "continue"
  125. ) > $gdb_in
  126. $gdb -batch -nx -x $gdb_in > /dev/null 2>&1
  127. # make sure that QEMU is dead
  128. # on OS X, exiting gdb doesn't always exit qemu
  129. kill $pid > /dev/null 2>&1
  130. fi
  131. }
  132. build_run() {
  133. # usage: build_run <tag> <args>
  134. show_build_tag "$1"
  135. shift
  136. if $verbose; then
  137. echo "$make $@ ..."
  138. fi
  139. $make $makeopts $@ 'DEFS+=-DDEBUG_GRADE' > $out 2> $err
  140. if [ $? -ne 0 ]; then
  141. echo $make $@ failed
  142. exit 1
  143. fi
  144. # now run qemu and save the output
  145. run_qemu
  146. show_time
  147. }
  148. check_result() {
  149. # usage: check_result <tag> <check> <check args...>
  150. show_check_tag "$1"
  151. shift
  152. # give qemu some time to run (for asynchronous mode)
  153. if [ ! -s $qemu_out ]; then
  154. sleep 4
  155. fi
  156. if [ ! -s $qemu_out ]; then
  157. fail > /dev/null
  158. echo 'no $qemu_out'
  159. else
  160. check=$1
  161. shift
  162. $check "$@"
  163. fi
  164. }
  165. check_regexps() {
  166. okay=yes
  167. not=0
  168. reg=0
  169. error=
  170. for i do
  171. if [ "x$i" = "x!" ]; then
  172. not=1
  173. elif [ "x$i" = "x-" ]; then
  174. reg=1
  175. else
  176. if [ $reg -ne 0 ]; then
  177. $grep '-E' "^$i\$" $qemu_out > /dev/null
  178. else
  179. $grep '-F' "$i" $qemu_out > /dev/null
  180. fi
  181. found=$(($? == 0))
  182. if [ $found -eq $not ]; then
  183. if [ $found -eq 0 ]; then
  184. msg="!! error: missing '$i'"
  185. else
  186. msg="!! error: got unexpected line '$i'"
  187. fi
  188. okay=no
  189. if [ -z "$error" ]; then
  190. error="$msg"
  191. else
  192. error="$error\n$msg"
  193. fi
  194. fi
  195. not=0
  196. reg=0
  197. fi
  198. done
  199. if [ "$okay" = "yes" ]; then
  200. pass
  201. else
  202. fail "$error"
  203. if $verbose; then
  204. exit 1
  205. fi
  206. fi
  207. }
  208. run_test() {
  209. # usage: run_test [-tag <tag>] [-prog <prog>] [-Ddef...] [-check <check>] checkargs ...
  210. tag=
  211. prog=
  212. check=check_regexps
  213. while true; do
  214. select=
  215. case $1 in
  216. -tag|-prog)
  217. select=`expr substr $1 2 ${#1}`
  218. eval $select='$2'
  219. ;;
  220. esac
  221. if [ -z "$select" ]; then
  222. break
  223. fi
  224. shift
  225. shift
  226. done
  227. defs=
  228. while expr "x$1" : "x-D.*" > /dev/null; do
  229. defs="DEFS+='$1' $defs"
  230. shift
  231. done
  232. if [ "x$1" = "x-check" ]; then
  233. check=$2
  234. shift
  235. shift
  236. fi
  237. if [ -z "$prog" ]; then
  238. $make $makeopts touch > /dev/null 2>&1
  239. args="$defs"
  240. else
  241. if [ -z "$tag" ]; then
  242. tag="$prog"
  243. fi
  244. args="build-$prog $defs"
  245. fi
  246. build_run "$tag" "$args"
  247. check_result 'check result' "$check" "$@"
  248. }
  249. quick_run() {
  250. # usage: quick_run <tag> [-Ddef...]
  251. tag="$1"
  252. shift
  253. defs=
  254. while expr "x$1" : "x-D.*" > /dev/null; do
  255. defs="DEFS+='$1' $defs"
  256. shift
  257. done
  258. $make $makeopts touch > /dev/null 2>&1
  259. build_run "$tag" "$defs"
  260. }
  261. quick_check() {
  262. # usage: quick_check <tag> checkargs ...
  263. tag="$1"
  264. shift
  265. check_result "$tag" check_regexps "$@"
  266. }
  267. ## kernel image
  268. osimg=$(make_print ucoreimg)
  269. ## sfs image
  270. sfsimg=$(make_print sfsimg)
  271. ## swap image
  272. swapimg=$(make_print swapimg)
  273. ## set default qemu-options
  274. qemuopts="-hda $osimg -drive file=$swapimg,media=disk,cache=writeback -drive file=$sfsimg,media=disk,cache=writeback"
  275. ## set break-function, default is readline
  276. brkfun=readline
  277. default_check() {
  278. pts=7
  279. check_regexps "$@"
  280. pts=3
  281. quick_check 'check output' \
  282. 'memory management: default_pmm_manager' \
  283. 'check_alloc_page() succeeded!' \
  284. 'check_pgdir() succeeded!' \
  285. 'check_boot_pgdir() succeeded!' \
  286. 'PDE(0e0) c0000000-f8000000 38000000 urw' \
  287. ' |-- PTE(38000) c0000000-f8000000 38000000 -rw' \
  288. 'PDE(001) fac00000-fb000000 00400000 -rw' \
  289. ' |-- PTE(000e0) faf00000-fafe0000 000e0000 urw' \
  290. ' |-- PTE(00001) fafeb000-fafec000 00001000 -rw' \
  291. 'check_slob() succeeded!' \
  292. 'check_vma_struct() succeeded!' \
  293. 'page fault at 0x00000100: K/W [no page found].' \
  294. 'check_pgfault() succeeded!' \
  295. 'check_vmm() succeeded.' \
  296. 'page fault at 0x00001000: K/W [no page found].' \
  297. 'page fault at 0x00002000: K/W [no page found].' \
  298. 'page fault at 0x00003000: K/W [no page found].' \
  299. 'page fault at 0x00004000: K/W [no page found].' \
  300. 'write Virt Page e in fifo_check_swap' \
  301. 'page fault at 0x00005000: K/W [no page found].' \
  302. 'page fault at 0x00001000: K/W [no page found]' \
  303. 'page fault at 0x00002000: K/W [no page found].' \
  304. 'page fault at 0x00003000: K/W [no page found].' \
  305. 'page fault at 0x00004000: K/W [no page found].' \
  306. 'check_swap() succeeded!' \
  307. '++ setup timer interrupts'
  308. }
  309. ## check now!!
  310. run_test -prog 'badsegment' -check default_check \
  311. - 'kernel_execve: pid = ., name = "badsegment".*' \
  312. - 'trapframe at 0xc.......' \
  313. 'trap 0x0000000d General Protection' \
  314. ' err 0x00000028' \
  315. - ' eip 0x008.....' \
  316. - ' esp 0xaff.....' \
  317. ' cs 0x----001b' \
  318. ' ss 0x----0023' \
  319. ! - 'user panic at .*'
  320. run_test -prog 'divzero' -check default_check \
  321. - 'kernel_execve: pid = ., name = "divzero".*' \
  322. - 'trapframe at 0xc.......' \
  323. 'trap 0x00000000 Divide error' \
  324. - ' eip 0x008.....' \
  325. - ' esp 0xaff.....' \
  326. ' cs 0x----001b' \
  327. ' ss 0x----0023' \
  328. ! - 'user panic at .*'
  329. run_test -prog 'softint' -check default_check \
  330. - 'kernel_execve: pid = ., name = "softint".*' \
  331. - 'trapframe at 0xc.......' \
  332. 'trap 0x0000000d General Protection' \
  333. ' err 0x00000072' \
  334. - ' eip 0x008.....' \
  335. - ' esp 0xaff.....' \
  336. ' cs 0x----001b' \
  337. ' ss 0x----0023' \
  338. ! - 'user panic at .*'
  339. pts=10
  340. run_test -prog 'faultread' -check default_check \
  341. - 'kernel_execve: pid = ., name = "faultread".*' \
  342. - 'trapframe at 0xc.......' \
  343. 'trap 0x0000000e Page Fault' \
  344. ' err 0x00000004' \
  345. - ' eip 0x008.....' \
  346. ! - 'user panic at .*'
  347. run_test -prog 'faultreadkernel' -check default_check \
  348. - 'kernel_execve: pid = ., name = "faultreadkernel".*' \
  349. - 'trapframe at 0xc.......' \
  350. 'trap 0x0000000e Page Fault' \
  351. ' err 0x00000005' \
  352. - ' eip 0x008.....' \
  353. ! - 'user panic at .*'
  354. run_test -prog 'hello' -check default_check \
  355. - 'kernel_execve: pid = ., name = "hello".*' \
  356. 'Hello world!!.' \
  357. - 'I am process .*' \
  358. 'hello pass.'
  359. run_test -prog 'testbss' -check default_check \
  360. - 'kernel_execve: pid = ., name = "testbss".*' \
  361. 'Making sure bss works right...' \
  362. 'Yes, good. Now doing a wild write off the end...' \
  363. 'testbss may pass.' \
  364. - 'trapframe at 0xc.......' \
  365. 'trap 0x0000000e Page Fault' \
  366. ' err 0x00000006' \
  367. - ' eip 0x008.....' \
  368. 'killed by kernel.' \
  369. ! - 'user panic at .*'
  370. run_test -prog 'pgdir' -check default_check \
  371. - 'kernel_execve: pid = ., name = "pgdir".*' \
  372. - 'I am .*' \
  373. 'PDE(001) 00800000-00c00000 00400000 urw' \
  374. ' |-- PTE(00002) 00800000-00802000 00002000 ur-' \
  375. ' |-- PTE(00001) 00802000-00803000 00001000 urw' \
  376. 'PDE(001) afc00000-b0000000 00400000 urw' \
  377. ' |-- PTE(00004) afffc000-b0000000 00004000 urw' \
  378. 'PDE(0e0) c0000000-f8000000 38000000 urw' \
  379. ' |-- PTE(38000) c0000000-f8000000 38000000 -rw' \
  380. 'pgdir pass.'
  381. run_test -prog 'yield' -check default_check \
  382. - 'kernel_execve: pid = ., name = "yield".*' \
  383. 'Hello, I am process 2.' \
  384. - 'Back in process ., iteration 0.' \
  385. - 'Back in process ., iteration 1.' \
  386. - 'Back in process ., iteration 2.' \
  387. - 'Back in process ., iteration 3.' \
  388. - 'Back in process ., iteration 4.' \
  389. - 'All done in process .*' \
  390. 'yield pass.'
  391. run_test -prog 'badarg' -check default_check \
  392. - 'kernel_execve: pid = ., name = "badarg".*' \
  393. 'fork ok.' \
  394. 'badarg pass.' \
  395. 'all user-mode processes have quit.' \
  396. 'init check memory pass.' \
  397. ! - 'user panic at .*'
  398. pts=10
  399. run_test -prog 'exit' -check default_check \
  400. - 'kernel_execve: pid = ., name = "exit".*' \
  401. 'I am the parent. Forking the child...' \
  402. 'I am the parent, waiting now..' \
  403. 'I am the child.' \
  404. - 'waitpid [0-9]+ ok\.' \
  405. 'exit pass.' \
  406. 'all user-mode processes have quit.' \
  407. 'init check memory pass.' \
  408. ! - 'user panic at .*'
  409. run_test -prog 'spin' -check default_check \
  410. - 'kernel_execve: pid = ., name = "spin".*' \
  411. 'I am the parent. Forking the child...' \
  412. 'I am the parent. Running the child...' \
  413. 'I am the child. spinning ...' \
  414. 'I am the parent. Killing the child...' \
  415. 'kill returns 0' \
  416. 'wait returns 0' \
  417. 'spin may pass.' \
  418. 'all user-mode processes have quit.' \
  419. 'init check memory pass.' \
  420. ! - 'user panic at .*'
  421. run_test -prog 'waitkill' -check default_check \
  422. - 'kernel_execve: pid = ., name = "waitkill".*' \
  423. 'wait child 1.' \
  424. 'child 2.' \
  425. 'child 1.' \
  426. 'kill parent ok.' \
  427. 'kill child1 ok.' \
  428. 'all user-mode processes have quit.' \
  429. 'init check memory pass.' \
  430. ! - 'user panic at .*'
  431. pts=15
  432. run_test -prog 'forktest' -check default_check \
  433. - 'kernel_execve: pid = ., name = "forktest".*' \
  434. 'I am child 31' \
  435. 'I am child 19' \
  436. 'I am child 13' \
  437. 'I am child 0' \
  438. 'forktest pass.' \
  439. 'all user-mode processes have quit.' \
  440. 'init check memory pass.' \
  441. ! - 'fork claimed to work [0-9]+ times!' \
  442. ! 'wait stopped early' \
  443. ! 'wait got too many' \
  444. ! - 'user panic at .*'
  445. pts=10
  446. run_test -prog 'forktree' -check default_check \
  447. - 'kernel_execve: pid = ., name = "forktree".*' \
  448. - '....: I am '\'''\' \
  449. - '....: I am '\''0'\' \
  450. - '....: I am '\'''\' \
  451. - '....: I am '\''1'\' \
  452. - '....: I am '\''0'\' \
  453. - '....: I am '\''01'\' \
  454. - '....: I am '\''00'\' \
  455. - '....: I am '\''11'\' \
  456. - '....: I am '\''10'\' \
  457. - '....: I am '\''101'\' \
  458. - '....: I am '\''100'\' \
  459. - '....: I am '\''111'\' \
  460. - '....: I am '\''110'\' \
  461. - '....: I am '\''001'\' \
  462. - '....: I am '\''000'\' \
  463. - '....: I am '\''011'\' \
  464. - '....: I am '\''010'\' \
  465. - '....: I am '\''0101'\' \
  466. - '....: I am '\''0100'\' \
  467. - '....: I am '\''0111'\' \
  468. - '....: I am '\''0110'\' \
  469. - '....: I am '\''0001'\' \
  470. - '....: I am '\''0000'\' \
  471. - '....: I am '\''0011'\' \
  472. - '....: I am '\''0010'\' \
  473. - '....: I am '\''1101'\' \
  474. - '....: I am '\''1100'\' \
  475. - '....: I am '\''1111'\' \
  476. - '....: I am '\''1110'\' \
  477. - '....: I am '\''1001'\' \
  478. - '....: I am '\''1000'\' \
  479. - '....: I am '\''1011'\' \
  480. - '....: I am '\''1010'\' \
  481. 'all user-mode processes have quit.' \
  482. 'init check memory pass.'
  483. pts=20
  484. timeout=150
  485. run_test -prog 'priority' -check default_check \
  486. 'sched class: stride_scheduler' \
  487. - 'kernel_execve: pid = ., name = "priority".*' \
  488. 'main: fork ok,now need to wait pids.' \
  489. 'stride sched correct result: 1 2 3 4 5' \
  490. 'all user-mode processes have quit.' \
  491. 'init check memory pass.' \
  492. ! - 'user panic at .*'
  493. pts=20
  494. timeout=240
  495. run_test -prog 'sleep' -check default_check \
  496. - 'kernel_execve: pid = ., name = "sleep".*' \
  497. 'sleep 1 x 100 slices.' \
  498. 'sleep 3 x 100 slices.' \
  499. 'sleep 7 x 100 slices.' \
  500. 'sleep 10 x 100 slices.' \
  501. - 'use 1... msecs.' \
  502. 'sleep pass.' \
  503. 'all user-mode processes have quit.' \
  504. 'init check memory pass.' \
  505. ! ' trap 0x0000000e Page Fault' \
  506. ! 'killed by kernel.' \
  507. ! - 'user panic at .*'
  508. pts=20
  509. timeout=240
  510. run_test -prog 'sleepkill' -check default_check \
  511. - 'kernel_execve: pid = ., name = "sleepkill".*' \
  512. 'sleepkill pass.' \
  513. 'all user-mode processes have quit.' \
  514. 'init check memory pass.' \
  515. ! - 'user panic at .*'
  516. pts=40
  517. timeout=500
  518. run_test -prog 'matrix' -check default_check \
  519. 'Iter 1, No.0 philosopher_sema is thinking' \
  520. 'Iter 1, No.1 philosopher_sema is thinking' \
  521. 'Iter 1, No.2 philosopher_sema is thinking' \
  522. 'Iter 1, No.3 philosopher_sema is thinking' \
  523. 'Iter 1, No.4 philosopher_sema is thinking' \
  524. 'Iter 1, No.0 philosopher_sema is eating' \
  525. 'Iter 1, No.1 philosopher_sema is eating' \
  526. 'Iter 1, No.2 philosopher_sema is eating' \
  527. 'Iter 1, No.3 philosopher_sema is eating' \
  528. 'Iter 1, No.4 philosopher_sema is eating' \
  529. 'No.0 philosopher_sema quit' \
  530. 'No.1 philosopher_sema quit' \
  531. 'No.2 philosopher_sema quit' \
  532. 'No.3 philosopher_sema quit' \
  533. 'No.4 philosopher_sema quit' \
  534. 'Iter 1, No.0 philosopher_condvar is thinking' \
  535. 'Iter 1, No.1 philosopher_condvar is thinking' \
  536. 'Iter 1, No.2 philosopher_condvar is thinking' \
  537. 'Iter 1, No.3 philosopher_condvar is thinking' \
  538. 'Iter 1, No.4 philosopher_condvar is thinking' \
  539. 'Iter 1, No.0 philosopher_condvar is eating' \
  540. 'Iter 1, No.1 philosopher_condvar is eating' \
  541. 'Iter 1, No.2 philosopher_condvar is eating' \
  542. 'Iter 1, No.3 philosopher_condvar is eating' \
  543. 'Iter 1, No.4 philosopher_condvar is eating' \
  544. 'No.0 philosopher_condvar quit' \
  545. 'No.1 philosopher_condvar quit' \
  546. 'No.2 philosopher_condvar quit' \
  547. 'No.3 philosopher_condvar quit' \
  548. 'No.4 philosopher_condvar quit' \
  549. - 'kernel_execve: pid = ., name = "matrix".*' \
  550. 'fork ok.' \
  551. 'pid 13 done!.' \
  552. 'pid 17 done!.' \
  553. 'pid 23 done!.' \
  554. 'matrix pass.' \
  555. 'all user-mode processes have quit.' \
  556. 'init check memory pass.' \
  557. ! - 'user panic at .*'
  558. ## print final-score
  559. show_final