56microcode = ''' 57def macroop IRET_REAL { 58 panic "Real mode iret isn't implemented!" 59}; 60 61def macroop IRET_PROT { 62 .adjust_env oszIn64Override 63 64 # Check for a nested task. This isn't supported at the moment. 65 rflag t1, NT 66 panic "Task switching with iret is unimplemented!", flags=(nCEZF,) 67 68 #t1 = temp_RIP 69 #t2 = temp_CS 70 #t3 = temp_RFLAGS 71 #t4 = handy m5 register 72 73 # Pop temp_RIP, temp_CS, and temp_RFLAGS 74 ld t1, ss, [1, t0, rsp], "0 * env.stackSize", dataSize=ssz 75 ld t2, ss, [1, t0, rsp], "1 * env.stackSize", dataSize=ssz 76 ld t3, ss, [1, t0, rsp], "2 * env.stackSize", dataSize=ssz 77 78 79 80### 81### Handle if we're returning to virtual 8086 mode. 82### 83 84 #IF ((temp_RFLAGS.VM=1) && (CPL=0) && (LEGACY_MODE)) 85 # IRET_FROM_PROTECTED_TO_VIRTUAL 86 87 #temp_RFLAGS.VM != 1 88 rcri t0, t3, 18, flags=(ECF,) 89 bri t0, label("protToVirtFallThrough"), flags=(nCECF,) 90 91 #CPL=0 92 rdm5reg t4 93 andi t0, t4, 0x30, flags=(EZF,) 94 bri t0, label("protToVirtFallThrough"), flags=(nCEZF,) 95 96 #(LEGACY_MODE) 97 rcri t0, t4, 1, flags=(ECF,) 98 bri t0, label("protToVirtFallThrough"), flags=(nCECF,) 99 100 panic "iret to virtual mode not supported" 101 102protToVirtFallThrough: 103 104 105 106 #temp_CPL = temp_CS.rpl 107 andi t5, t2, 0x3 108 109 110### 111### Read in the info for the new CS segment. 112### 113 114 #CS = READ_DESCRIPTOR (temp_CS, iret_chk) 115 andi t0, t2, 0xFC, flags=(EZF,), dataSize=2 116 bri t0, label("processCSDescriptor"), flags=(CEZF,) 117 andi t6, t2, 0xF8, dataSize=8 118 andi t0, t2, 0x4, flags=(EZF,), dataSize=2 119 bri t0, label("globalCSDescriptor"), flags=(CEZF,) 120 ld t6, tsl, [1, t0, t6], dataSize=8 121 bri t0, label("processCSDescriptor") 122globalCSDescriptor: 123 ld t6, tsg, [1, t0, t6], dataSize=8 124processCSDescriptor: 125 chks t2, t6, dataSize=8 126 127 # This actually updates state which is wrong. It should wait until we know 128 # we're not going to fault. Unfortunately, that's hard to do. 129 wrdl cs, t6, t2 130 wrsel cs, t2 131 132 #CPL = temp_CPL 133 134 135### 136### Get the new stack pointer and stack segment off the old stack if necessary, 137### and piggyback on the logic to check the new RIP value. 138### 139 #IF ((64BIT_MODE) || (temp_CPL!=CPL)) 140 #{ 141 142 #(64BIT_MODE) 143 andi t0, t4, 0xE, flags=(EZF,) 144 # Since we just found out we're in 64 bit mode, take advantage and 145 # do the appropriate RIP checks. 146 bri t0, label("doPopStackStuffAndCheckRIP"), flags=(CEZF,) 147 148 # Here, we know we're -not- in 64 bit mode, so we should do the 149 # appropriate/other RIP checks. 150 # if temp_RIP > CS.limit throw #GP(0) 151 rdlimit t6, cs 152 subi t0, t1, t6, flags=(ECF,) 153 fault "new GeneralProtection(0)", flags=(CECF,) 154 155 #(temp_CPL!=CPL) 156 srli t7, t4, 4 157 xor t7, t7, t5 158 andi t0, t7, 0x3, flags=(EZF,) 159 bri t0, label("doPopStackStuff"), flags=(nCEZF,) 160 # We can modify user visible state here because we're know 161 # we're done with things that can fault. 162 addi rsp, rsp, "3 * env.stackSize" 163 bri t0, label("fallThroughPopStackStuff") 164 165doPopStackStuffAndCheckRIP: 166 # Check if the RIP is canonical. 167 sra t7, t1, 47, flags=(EZF,), dataSize=ssz 168 # if t7 isn't 0 or -1, it wasn't canonical. 169 bri t0, label("doPopStackStuff"), flags=(CEZF,) 170 addi t0, t7, 1, flags=(EZF,), dataSize=ssz 171 fault "new GeneralProtection(0)", flags=(nCEZF,) 172 173doPopStackStuff: 174 # POP.v temp_RSP 175 ld t6, ss, [1, t0, rsp], "3 * env.dataSize", dataSize=ssz 176 # POP.v temp_SS 177 ld t2, ss, [1, t0, rsp], "4 * env.dataSize", dataSize=ssz 178 # SS = READ_DESCRIPTOR (temp_SS, ss_chk) 179 andi t0, t2, 0xFC, flags=(EZF,), dataSize=2 180 bri t0, label("processSSDescriptor"), flags=(CEZF,) 181 andi t7, t2, 0xF8, dataSize=8 182 andi t0, t2, 0x4, flags=(EZF,), dataSize=2 183 bri t0, label("globalSSDescriptor"), flags=(CEZF,) 184 ld t7, tsl, [1, t0, t7], dataSize=8 185 bri t0, label("processSSDescriptor") 186globalSSDescriptor: 187 ld t7, tsg, [1, t0, t7], dataSize=8 188processSSDescriptor: 189 chks t2, t7, dataSize=8 190 191 # This actually updates state which is wrong. It should wait until we know 192 # we're not going to fault. Unfortunately, that's hard to do. 193 wrdl cs, t7, t2 194 wrsel cs, t2 195 196### 197### From this point downwards, we can't fault. We can update user visible state. 198### 199 # RSP.s = temp_RSP 200 mov rsp, rsp, t6, dataSize=ssz 201 202 #} 203 204fallThroughPopStackStuff: 205 206 #IF (changing CPL) 207 #{ 208 srli t7, t4, 4 209 xor t7, t7, t5 210 andi t0, t7, 0x3, flags=(EZF,) 211 bri t0, label("skipSegmentSquashing"), flags=(CEZF,) 212 213 # The attribute register needs to keep track of more info before this will 214 # work the way it needs to. 215 # FOR (seg = ES, DS, FS, GS) 216 # IF ((seg.attr.dpl < cpl && ((seg.attr.type = 'data') 217 # || (seg.attr.type = 'non-conforming-code'))) 218 # { 219 # seg = NULL 220 # } 221 #} 222 223skipSegmentSquashing: 224 225 # Ignore this for now. 226 #RFLAGS.v = temp_RFLAGS 227 # VIF,VIP,IOPL only changed if (old_CPL = 0) 228 # IF only changed if (old_CPL <= old_RFLAGS.IOPL) 229 # VM unchanged 230 # RF cleared 231 232 #RIP = temp_RIP 233 wrip t0, t1, dataSize=ssz 234}; 235 236def macroop IRET_VIRT { 237 panic "Virtual mode iret isn't implemented!" 238}; 239''' |