《操作系统》的实验代码。
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.

160 lines
5.1 KiB

  1. #include <stdio.h>
  2. #define STS_IG32 0xE // 32-bit Interrupt Gate
  3. #define STS_TG32 0xF // 32-bit Trap Gate
  4. typedef unsigned uint32_t;
  5. #define SETGATE(gate, istrap, sel, off, dpl) { \
  6. (gate).gd_off_15_0 = (uint32_t)(off) & 0xffff; \
  7. (gate).gd_ss = (sel); \
  8. (gate).gd_args = 0; \
  9. (gate).gd_rsv1 = 0; \
  10. (gate).gd_type = (istrap) ? STS_TG32 : STS_IG32; \
  11. (gate).gd_s = 0; \
  12. (gate).gd_dpl = (dpl); \
  13. (gate).gd_p = 1; \
  14. (gate).gd_off_31_16 = (uint32_t)(off) >> 16; \
  15. }
  16. /* Gate descriptors for interrupts and traps */
  17. struct gatedesc {
  18. unsigned gd_off_15_0 : 16; // low 16 bits of offset in segment
  19. unsigned gd_ss : 16; // segment selector
  20. unsigned gd_args : 5; // # args, 0 for interrupt/trap gates
  21. unsigned gd_rsv1 : 3; // reserved(should be zero I guess)
  22. unsigned gd_type : 4; // type(STS_{TG,IG32,TG32})
  23. unsigned gd_s : 1; // must be 0 (system)
  24. unsigned gd_dpl : 2; // descriptor(meaning new) privilege level
  25. unsigned gd_p : 1; // Present
  26. unsigned gd_off_31_16 : 16; // high bits of offset in segment
  27. };
  28. int
  29. main(void)
  30. {
  31. unsigned before;
  32. unsigned intr;
  33. unsigned after;
  34. struct gatedesc gintr;
  35. intr=8;
  36. before=after=0;
  37. gintr=*((struct gatedesc *)&intr);
  38. SETGATE(gintr, 0,1,2,3);
  39. intr=*(unsigned *)&(gintr);
  40. printf("intr is 0x%x\n",intr);
  41. printf("gintr is 0x%llx\n",gintr);
  42. return 0;
  43. }
  44. // other examples
  45. //ex1
  46. #if 0
  47. #include <stdlib.h>
  48. #include <stdio.h>
  49. #include <iostream>
  50. #include <cstring>
  51. #define STS_TG32 0xF
  52. #define STS_IG32 0xE
  53. #define SETGATE( gate, istrap, sel, off, dpl) { \
  54. (gate).gd_off_15_0 = (uint32_t)(off) & 0xffff; \
  55. (gate).gd_ss = (sel); \
  56. (gate).gd_args = 0; \
  57. (gate).gd_rsv1 = 0; \
  58. (gate).gd_type = (istrap) ? STS_TG32 : STS_IG32; \
  59. (gate).gd_s = 0; \
  60. (gate).gd_dpl = (dpl); \
  61. (gate).gd_p = 1; \
  62. (gate).gd_off_31_16 = (uint32_t)(off) >> 16; \
  63. }
  64. using namespace std;
  65. struct gatedesc {
  66. unsigned gd_off_15_0 : 16; // low 16 bits of offset in segment
  67. unsigned gd_ss : 16; // segment selector
  68. unsigned gd_args : 5; // # args, 0 for interrupt/trap gates
  69. unsigned gd_rsv1 : 3; // reserved(should be zero I guess)
  70. unsigned gd_type : 4; // type(STS_{TG,IG32,TG32})
  71. unsigned gd_s : 1; // must be 0 (system)
  72. unsigned gd_dpl : 2; // descriptor(meaning new) privilege level
  73. unsigned gd_p : 1; // Present
  74. unsigned gd_off_31_16 : 16; // high bits of offset in segment
  75. };
  76. int main()
  77. {
  78. gatedesc intr;
  79. intr.gd_off_15_0 = 8;
  80. intr.gd_ss = 0;
  81. intr.gd_args = 0;
  82. intr.gd_rsv1 = 0;
  83. intr.gd_type = 0;
  84. intr.gd_s = 0;
  85. intr.gd_dpl = 0;
  86. intr.gd_p = 0;
  87. intr.gd_off_31_16 = 0;
  88. SETGATE( intr, 0,1,2,3);
  89. printf( "%u\n", intr);
  90. printf( "%x", intr);
  91. return 0;
  92. }
  93. #endif
  94. //ex2
  95. #if 0
  96. #include "stdlib.h"
  97. struct gatedesc {
  98. unsigned gd_off_15_0 : 16; // low 16 bits of offset in segment
  99. unsigned gd_ss : 16; // segment selector
  100. unsigned gd_args : 5; // # args, 0 for interrupt/trap gates
  101. unsigned gd_rsv1 : 3; // reserved(should be zero I guess)
  102. unsigned gd_type : 4; // type(STS_{TG,IG32,TG32})
  103. unsigned gd_s : 1; // must be 0 (system)
  104. unsigned gd_dpl : 2; // descriptor(meaning new) privilege level
  105. unsigned gd_p : 1; // Present
  106. unsigned gd_off_31_16 : 16; // high bits of offset in segment
  107. };
  108. typedef struct gatedesc gatedesc;
  109. typedef unsigned int uint32_t;
  110. #define STS_IG32 0xE // 32-bit Interrupt Gate
  111. #define STS_TG32 0xF // 32-bit Trap Gate
  112. #define SETGATE(gate, istrap, sel, off, dpl) { \
  113. ((gatedesc*)(&gate))->gd_off_15_0 = (uint32_t)(off) & 0xffff; \
  114. ((gatedesc*)(&gate))->gd_ss = (sel); \
  115. ((gatedesc*)(&gate))->gd_args = 0; \
  116. ((gatedesc*)(&gate))->gd_rsv1 = 0; \
  117. ((gatedesc*)(&gate))->gd_type = (istrap) ? STS_TG32 : STS_IG32; \
  118. ((gatedesc*)(&gate))->gd_s = 0; \
  119. ((gatedesc*)(&gate))->gd_dpl = (dpl); \
  120. ((gatedesc*)(&gate))->gd_p = 1; \
  121. ((gatedesc*)(&gate))->gd_off_31_16 = (uint32_t)(off) >> 16; \
  122. }
  123. int main()
  124. {
  125. unsigned intr;
  126. intr=8;
  127. SETGATE(intr, 0,1,2,3);
  128. printf("%d", intr);
  129. return 0;
  130. }
  131. #endif