From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CBE48606A32C for ; Fri, 4 Dec 2026 21:27:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ZaPeD8F5 Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xZao4-0001zj-BF; Fri, 04 Dec 2026 16:27:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xZao0-0001yz-5d for qemu-devel@nongnu.org; Fri, 04 Dec 2026 16:27:00 -0500 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xZanu-0006pE-R7 for qemu-devel@nongnu.org; Fri, 04 Dec 2026 16:26:59 -0500 Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-4acb44dcd7bso1488080f8f.0 for ; Fri, 04 Dec 2026 13:26:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796419613; x=1797024413; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=vsfHf/z4rOpztPUI1r54VNFnNw2MBbRcKjME6CZa0fk=; b=ZaPeD8F5HPS1NEVShq269OA3hwpO7mKGHOKy/oebnhpe7jyGkZk/8H3+AwMbyWt/I2 iOxnPkgivbGL+rGCVXUQrW8eVZ+eX5pK7+FFo5D3tS3XTkjvsBwcN/208Jf9247wW7Ju rqdzV5FiUf2gKSbM4FS/oj4bUeugaXvXI/7ywHuya7Wts+ag6tGyOUiB+kHVuK4wJFIo IcPnUQuswGQmRIXP1GP8Gj9gmja1yNZecdBVAr0j1wZylVP6Hgxe2l9K4wB2JEXot3il SVSWbvW8u60MKj4NLxJX66/PveHbz11vLGUo2AyTXHNM/2qxsr51Iq5H+2yUoDl5ohQN raWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796419613; x=1797024413; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=vsfHf/z4rOpztPUI1r54VNFnNw2MBbRcKjME6CZa0fk=; b=kXEVT2HUdtAy+68qbDuSfrSJ0EbAVLrvHhS9o75jnZJ1c45uyMRKOH21SUE1OVgP3D 2zscmtiwsdk7GpHGJ/ekGfo5CxXuuHMqoOU0Ee12GUbCtwH1J0xp6iNJah6yFavVy57V 60iIONLzePxv4QNWKFZN1kKQ1xiC0vLLDOG68b3xr7Xo5apgBNLhxkcj9IfSYANNAl9N DyZb5L1Z++qg/HGZsWO/0FCvJxk5IUi6RxrJGsM7LGsx9psV4/jiFVs+ENkifhVt8ln4 AMFP6ffYG8mwECQtE1cWgSbXigEK6hlxBGYYmSX9bTZVZQbYCrAe+XhtYuQ0eO2muBa/ lPMw== X-Gm-Message-State: AB4q+uEiAU6OpAoHEAHH2fIUA1mLjri523qGGMRM9+KjnnZKMk7IUPR9 wbfNPTW04cncNO/aEx5H/AL8co/s4TMCcOi1aj6PAg6ulu8wL7N3CRdqknf3a8VRHus= X-Gm-Gg: AZFdTZfRZ0mxyX1B8A5wT9Srr3GhtYe7R8zGjZ5sMVqauEDVy9N4OQ3HHB4e0yBPjcR SrhHuf2AFOV3HyXBFWcIUf9PE8ZsEFdqUkHzqZm9bL3BQJKMmOyIPgmpcwaf7ZjytQ1ZLYf4igg 0FEzT27bluX+Dm7Svl5HE2dBESxzLASLnhYwk8TmSJDOm476qOwzUaw3BWJ0e6UWGEvGmgXBq3I bdRGXOyUx+BjG7scRLjD59j2Eu5U1qo+vxRs7M+24EXRRgqAzBxdvVyaaCSrb670nxBYlYmtnm7 JU0mzlffVIl0D/y4AaIQUtffORsxCcLvoPXz/0+/VT1JUCg4/Q6IZNEKFyqvOXHrgTsQWmwLHEv cdQz/7EVkMNAM1Reux+Cqoh1ac9ORQq0ONKkE0r/6jBaQQwkyrS6P/FfT6pDfMUFT+xQzxKo8j3 Wob+4MQn0KWFm21RgKDHP5IOHJnZWMiHYDWrJUL3ZZ+0kg3XZqAMZMzVi4izVJhe+aXvh0a2JNm 2aoIs44Sty5qZJSYQfaQH5Fta0jxMHwVre3i5jpBFkt4yiXYo2/cb2OgjHUbLw8IznNueI+6IVa kH2K/VwQelPALz42n2fzbx+egnf1NMU7BSygGPkDTuNj06fmWmXus6Xcb3jvZnIdAgJRW9NprAE zo+za X-Received: by 2002:a05:6000:4311:b0:4d0:6f3e:382b with SMTP id ffacd0b85a97d-4d1dd3aed96mr5971417f8f.0.1796419613053; Fri, 04 Dec 2026 13:26:53 -0800 (PST) Received: from DESKTOP-ECOGLG7 (ppp079166142084.access.hol.gr. [79.166.142.84]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4d1723b8ab8sm7469726f8f.20.2026.12.04.13.26.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 13:26:51 -0800 (PST) From: Nick Sampanis X-Google-Original-From: Nick Sampanis To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Richard Henderson , Nick Sampanis Subject: [PATCH 3/3] target/i386/tcg: Set EXT bit in error codes during event delivery Date: Fri, 4 Dec 2026 23:26:40 +0200 Message-ID: <20261204212641.2770-4-Nicksampanis@gmail.com> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20261204212641.2770-1-Nicksampanis@gmail.com> References: <20261204212641.2770-1-Nicksampanis@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2a00:1450:4864:20::42b; envelope-from=nicksampanis@gmail.com; helo=mail-wr1-x42b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org X-Spam-Status: No, score=-16.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 The EXT flag (bit 0 of an exception error code) marks faults raised while delivering an event, including hardware interrupts and prior exceptions, while staying clear for software interrupts such as INT n, INT3, and INTO (Intel SDM Vol. 3A 7.13 "Error Code"). Currently, QEMU never sets EXT for faults raised during interrupt or exception delivery. As a result, error codes are inaccurate across IDT gate checks, target code-segment checks, TSS stack switching (#TS/#SS), and task-gate transitions (#TS/#NP/#GP). Vendor rules differ on when EXT applies (AMD64 APM Vol. 2 8.4.1 "Selector-Error Code"). On AMD, EXT is set when the exception source is external to the processor (is_hw), whereas Intel sets it for any event except software interrupts, including exceptions (!is_int). Add an ext argument for switch_tss_ra(), tss_load_seg(), get_ss_esp_from_tss(), and get_rsp_from_tss(). Signed-off-by: Nick Sampanis --- target/i386/tcg/seg_helper.c | 176 ++++++++++++++++++++--------------- 1 file changed, 102 insertions(+), 74 deletions(-) diff --git a/target/i386/tcg/seg_helper.c b/target/i386/tcg/seg_helper.c index 1a986ce7a9..ea1eb827e8 100644 --- a/target/i386/tcg/seg_helper.c +++ b/target/i386/tcg/seg_helper.c @@ -224,7 +224,7 @@ static inline void load_seg_vm(CPUX86State *env, int seg, int selector) static inline void get_ss_esp_from_tss(CPUX86State *env, uint32_t *ss_ptr, uint32_t *esp_ptr, int dpl, - uintptr_t retaddr) + uintptr_t retaddr, uint8_t ext) { X86CPU *cpu = env_archcpu(env); int type, index, shift; @@ -253,7 +253,8 @@ static inline void get_ss_esp_from_tss(CPUX86State *env, uint32_t *ss_ptr, shift = type >> 3; index = (dpl * 4 + 2) << shift; if (index + (4 << shift) - 1 > env->tr.limit) { - raise_exception_err_ra(env, EXCP0A_TSS, env->tr.selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0A_TSS, + (env->tr.selector & 0xfffc) | ext, retaddr); } if (shift == 0) { *esp_ptr = cpu_lduw_kernel_ra(env, env->tr.base + index, retaddr); @@ -265,49 +266,58 @@ static inline void get_ss_esp_from_tss(CPUX86State *env, uint32_t *ss_ptr, } static void tss_load_seg(CPUX86State *env, X86Seg seg_reg, int selector, - int cpl, uintptr_t retaddr) + int cpl, uintptr_t retaddr, uint8_t ext) { uint32_t e1, e2; int rpl, dpl; if ((selector & 0xfffc) != 0) { if (load_segment_ra(env, &e1, &e2, selector, retaddr) != 0) { - raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0A_TSS, (selector & 0xfffc) | ext, + retaddr); } if (!(e2 & DESC_S_MASK)) { - raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0A_TSS, (selector & 0xfffc) | ext, + retaddr); } rpl = selector & 3; dpl = (e2 >> DESC_DPL_SHIFT) & 3; if (seg_reg == R_CS) { if (!(e2 & DESC_CS_MASK)) { - raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0A_TSS, + (selector & 0xfffc) | ext, retaddr); } if (dpl != rpl) { - raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0A_TSS, + (selector & 0xfffc) | ext, retaddr); } } else if (seg_reg == R_SS) { /* SS must be writable data */ if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK)) { - raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0A_TSS, + (selector & 0xfffc) | ext, retaddr); } if (dpl != cpl || dpl != rpl) { - raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0A_TSS, + (selector & 0xfffc) | ext, retaddr); } } else { /* not readable code */ if ((e2 & DESC_CS_MASK) && !(e2 & DESC_R_MASK)) { - raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0A_TSS, + (selector & 0xfffc) | ext, retaddr); } /* if data or non conforming code, checks the rights */ if (((e2 >> DESC_TYPE_SHIFT) & 0xf) < 12) { if (dpl < cpl || dpl < rpl) { - raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0A_TSS, + (selector & 0xfffc) | ext, retaddr); } } } if (!(e2 & DESC_P_MASK)) { - raise_exception_err_ra(env, EXCP0B_NOSEG, selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0B_NOSEG, (selector & 0xfffc) | ext, + retaddr); } set_seg_accessed(env, selector, &e2, retaddr); cpu_x86_load_seg_cache(env, seg_reg, selector, @@ -316,7 +326,8 @@ static void tss_load_seg(CPUX86State *env, X86Seg seg_reg, int selector, e2); } else { if (seg_reg == R_SS || seg_reg == R_CS) { - raise_exception_err_ra(env, EXCP0A_TSS, selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0A_TSS, (selector & 0xfffc) | ext, + retaddr); } } } @@ -343,7 +354,7 @@ static void tss_set_busy(CPUX86State *env, int tss_selector, bool value, static void switch_tss_ra(CPUX86State *env, int tss_selector, uint32_t e1, uint32_t e2, int source, uint32_t next_eip, bool has_error_code, - uint32_t error_code, uintptr_t retaddr) + uint32_t error_code, uintptr_t retaddr, uint8_t ext) { int tss_limit, tss_limit_max, type, old_tss_limit_max, old_type, i; target_ulong tss_base; @@ -362,26 +373,32 @@ static void switch_tss_ra(CPUX86State *env, int tss_selector, /* if task gate, we read the TSS segment and we load it */ if (type == 5) { if (!(e2 & DESC_P_MASK)) { - raise_exception_err_ra(env, EXCP0B_NOSEG, tss_selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0B_NOSEG, + (tss_selector & 0xfffc) | ext, retaddr); } tss_selector = e1 >> 16; if (tss_selector & 4) { - raise_exception_err_ra(env, EXCP0A_TSS, tss_selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0A_TSS, + (tss_selector & 0xfffc) | ext, retaddr); } if (load_segment_ra(env, &e1, &e2, tss_selector, retaddr) != 0) { - raise_exception_err_ra(env, EXCP0D_GPF, tss_selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0D_GPF, + (tss_selector & 0xfffc) | ext, retaddr); } if (e2 & DESC_S_MASK) { - raise_exception_err_ra(env, EXCP0D_GPF, tss_selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0D_GPF, + (tss_selector & 0xfffc) | ext, retaddr); } type = (e2 >> DESC_TYPE_SHIFT) & 0xf; if ((type & 7) != 1) { - raise_exception_err_ra(env, EXCP0D_GPF, tss_selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0D_GPF, + (tss_selector & 0xfffc) | ext, retaddr); } } if (!(e2 & DESC_P_MASK)) { - raise_exception_err_ra(env, EXCP0B_NOSEG, tss_selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0B_NOSEG, (tss_selector & 0xfffc) | ext, + retaddr); } if (type & 8) { @@ -393,7 +410,8 @@ static void switch_tss_ra(CPUX86State *env, int tss_selector, tss_base = get_seg_base(e1, e2); if ((tss_selector & 4) != 0 || tss_limit < tss_limit_max) { - raise_exception_err_ra(env, EXCP0A_TSS, tss_selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0A_TSS, (tss_selector & 0xfffc) | ext, + retaddr); } old_type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf; if (old_type & 8) { @@ -404,7 +422,8 @@ static void switch_tss_ra(CPUX86State *env, int tss_selector, /* new TSS must be busy iff the source is an IRET instruction */ if (!!(e2 & DESC_TSS_BUSY_MASK) != (source == SWITCH_TSS_IRET)) { - raise_exception_err_ra(env, EXCP0A_TSS, tss_selector & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0A_TSS, (tss_selector & 0xfffc) | ext, + retaddr); } /* X86Access avoids memory exceptions during the task switch */ @@ -565,23 +584,27 @@ static void switch_tss_ra(CPUX86State *env, int tss_selector, /* load the LDT */ if (new_ldt & 4) { - raise_exception_err_ra(env, EXCP0A_TSS, new_ldt & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0A_TSS, (new_ldt & 0xfffc) | ext, + retaddr); } if ((new_ldt & 0xfffc) != 0) { dt = &env->gdt; index = new_ldt & ~7; if ((index + 7) > dt->limit) { - raise_exception_err_ra(env, EXCP0A_TSS, new_ldt & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0A_TSS, (new_ldt & 0xfffc) | ext, + retaddr); } ptr = dt->base + index; e1 = cpu_ldl_kernel_ra(env, ptr, retaddr); e2 = cpu_ldl_kernel_ra(env, ptr + 4, retaddr); if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2) { - raise_exception_err_ra(env, EXCP0A_TSS, new_ldt & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0A_TSS, (new_ldt & 0xfffc) | ext, + retaddr); } if (!(e2 & DESC_P_MASK)) { - raise_exception_err_ra(env, EXCP0A_TSS, new_ldt & 0xfffc, retaddr); + raise_exception_err_ra(env, EXCP0A_TSS, (new_ldt & 0xfffc) | ext, + retaddr); } load_seg_cache_raw_dt(&env->ldt, e1, e2); } @@ -589,18 +612,18 @@ static void switch_tss_ra(CPUX86State *env, int tss_selector, /* load the segments */ if (!(new_eflags & VM_MASK)) { int cpl = new_segs[R_CS] & 3; - tss_load_seg(env, R_CS, new_segs[R_CS], cpl, retaddr); - tss_load_seg(env, R_SS, new_segs[R_SS], cpl, retaddr); - tss_load_seg(env, R_ES, new_segs[R_ES], cpl, retaddr); - tss_load_seg(env, R_DS, new_segs[R_DS], cpl, retaddr); - tss_load_seg(env, R_FS, new_segs[R_FS], cpl, retaddr); - tss_load_seg(env, R_GS, new_segs[R_GS], cpl, retaddr); + tss_load_seg(env, R_CS, new_segs[R_CS], cpl, retaddr, ext); + tss_load_seg(env, R_SS, new_segs[R_SS], cpl, retaddr, ext); + tss_load_seg(env, R_ES, new_segs[R_ES], cpl, retaddr, ext); + tss_load_seg(env, R_DS, new_segs[R_DS], cpl, retaddr, ext); + tss_load_seg(env, R_FS, new_segs[R_FS], cpl, retaddr, ext); + tss_load_seg(env, R_GS, new_segs[R_GS], cpl, retaddr, ext); } /* check that env->eip is in the CS segment limits */ if (new_eip > env->segs[R_CS].limit) { /* XXX: different exception if CALL? */ - raise_exception_err_ra(env, EXCP0D_GPF, 0, retaddr); + raise_exception_err_ra(env, EXCP0D_GPF, ext, retaddr); } #ifndef CONFIG_USER_ONLY @@ -642,10 +665,10 @@ static void switch_tss_ra(CPUX86State *env, int tss_selector, static void switch_tss(CPUX86State *env, int tss_selector, uint32_t e1, uint32_t e2, int source, uint32_t next_eip, bool has_error_code, - int error_code) + int error_code, uint8_t ext) { switch_tss_ra(env, tss_selector, e1, e2, source, next_eip, - has_error_code, error_code, 0); + has_error_code, error_code, 0, ext); } static inline unsigned int get_sp_mask(unsigned int e2) @@ -709,7 +732,9 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int, int vm86 = env->eflags & VM_MASK; StackAccess sa; bool set_rf; + uint8_t ext; + ext = IS_AMD_CPU(env) ? !!is_hw : !is_int; has_error_code = 0; if (!is_int && !is_hw) { has_error_code = exception_has_error_code(intno); @@ -724,7 +749,7 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int, dt = &env->idt; if (intno * 8 + 7 > dt->limit) { - raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2); + raise_exception_err(env, EXCP0D_GPF, (intno * 8) | 2 | ext); } ptr = dt->base + intno * 8; e1 = cpu_ldl_kernel(env, ptr); @@ -739,14 +764,14 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int, case 15: /* 386 trap gate */ break; default: - raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2); + raise_exception_err(env, EXCP0D_GPF, (intno * 8) | 2 | ext); break; } dpl = (e2 >> DESC_DPL_SHIFT) & 3; cpl = env->hflags & HF_CPL_MASK; /* check privilege if software int */ if (is_int && dpl < cpl) { - raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2); + raise_exception_err(env, EXCP0D_GPF, (intno * 8) | 2 | ext); } sa.env = env; @@ -756,10 +781,10 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int, /* task gate */ /* must do that check here to return the correct error code */ if (!(e2 & DESC_P_MASK)) { - raise_exception_err(env, EXCP0B_NOSEG, intno * 8 + 2); + raise_exception_err(env, EXCP0B_NOSEG, (intno * 8) | 2 | ext); } switch_tss(env, intno * 8, e1, e2, SWITCH_TSS_CALL, old_eip, - has_error_code, error_code); + has_error_code, error_code, ext); return; } @@ -767,25 +792,25 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int, /* check valid bit */ if (!(e2 & DESC_P_MASK)) { - raise_exception_err(env, EXCP0B_NOSEG, intno * 8 + 2); + raise_exception_err(env, EXCP0B_NOSEG, (intno * 8) | 2 | ext); } selector = e1 >> 16; offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff); if ((selector & 0xfffc) == 0) { - raise_exception_err(env, EXCP0D_GPF, 0); + raise_exception_err(env, EXCP0D_GPF, ext); } if (load_segment(env, &e1, &e2, selector) != 0) { - raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); + raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext); } if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK))) { - raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); + raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext); } dpl = (e2 >> DESC_DPL_SHIFT) & 3; if (dpl > cpl) { - raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); + raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext); } if (!(e2 & DESC_P_MASK)) { - raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc); + raise_exception_err(env, EXCP0B_NOSEG, (selector & 0xfffc) | ext); } set_seg_accessed(env, selector, &e2, 0); if (e2 & DESC_C_MASK) { @@ -795,27 +820,27 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int, if (dpl < cpl) { /* to inner privilege */ uint32_t esp; - get_ss_esp_from_tss(env, &ss, &esp, dpl, 0); + get_ss_esp_from_tss(env, &ss, &esp, dpl, 0, ext); if ((ss & 0xfffc) == 0) { - raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); + raise_exception_err(env, EXCP0A_TSS, (ss & 0xfffc) | ext); } if ((ss & 3) != dpl) { - raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); + raise_exception_err(env, EXCP0A_TSS, (ss & 0xfffc) | ext); } if (load_segment(env, &ss_e1, &ss_e2, ss) != 0) { - raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); + raise_exception_err(env, EXCP0A_TSS, (ss & 0xfffc) | ext); } ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3; if (ss_dpl != dpl) { - raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); + raise_exception_err(env, EXCP0A_TSS, (ss & 0xfffc) | ext); } if (!(ss_e2 & DESC_S_MASK) || (ss_e2 & DESC_CS_MASK) || !(ss_e2 & DESC_W_MASK)) { - raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); + raise_exception_err(env, EXCP0A_TSS, (ss & 0xfffc) | ext); } if (!(ss_e2 & DESC_P_MASK)) { - raise_exception_err(env, EXCP0A_TSS, ss & 0xfffc); + raise_exception_err(env, EXCP0A_TSS, (ss & 0xfffc) | ext); } set_seg_accessed(env, ss, &ss_e2, 0); new_stack = 1; @@ -825,7 +850,7 @@ static void do_interrupt_protected(CPUX86State *env, int intno, int is_int, } else { /* to same privilege */ if (vm86) { - raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); + raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext); } new_stack = 0; sa.sp = env->regs[R_ESP]; @@ -930,7 +955,8 @@ static uint64_t popq(StackAccess *sa) return ret; } -static inline target_ulong get_rsp_from_tss(CPUX86State *env, int level) +static inline target_ulong get_rsp_from_tss(CPUX86State *env, int level, + uint8_t ext) { X86CPU *cpu = env_archcpu(env); int index, pg_mode; @@ -947,7 +973,7 @@ static inline target_ulong get_rsp_from_tss(CPUX86State *env, int level) } index = 8 * level + 4; if ((index + 7) > env->tr.limit) { - raise_exception_err(env, EXCP0A_TSS, env->tr.selector & 0xfffc); + raise_exception_err(env, EXCP0A_TSS, (env->tr.selector & 0xfffc) | ext); } rsp = cpu_ldq_kernel(env, env->tr.base + index); @@ -956,7 +982,7 @@ static inline target_ulong get_rsp_from_tss(CPUX86State *env, int level) pg_mode = get_pg_mode(env); sext = (int64_t)rsp >> (pg_mode & PG_MODE_LA57 ? 56 : 47); if (sext != 0 && sext != -1) { - raise_exception_err(env, EXCP0C_STACK, 0); + raise_exception_err(env, EXCP0C_STACK, ext); } return rsp; @@ -974,7 +1000,9 @@ static void do_interrupt64(CPUX86State *env, int intno, int is_int, target_ulong old_eip, offset; bool set_rf; StackAccess sa; + uint8_t ext; + ext = IS_AMD_CPU(env) ? !!is_hw : !is_int; has_error_code = 0; if (!is_int && !is_hw) { has_error_code = exception_has_error_code(intno); @@ -989,7 +1017,7 @@ static void do_interrupt64(CPUX86State *env, int intno, int is_int, dt = &env->idt; if (intno * 16 + 15 > dt->limit) { - raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2); + raise_exception_err(env, EXCP0D_GPF, (intno * 8) | 2 | ext); } ptr = dt->base + intno * 16; e1 = cpu_ldl_kernel(env, ptr); @@ -1002,41 +1030,41 @@ static void do_interrupt64(CPUX86State *env, int intno, int is_int, case 15: /* 386 trap gate */ break; default: - raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2); + raise_exception_err(env, EXCP0D_GPF, (intno * 8) | 2 | ext); break; } dpl = (e2 >> DESC_DPL_SHIFT) & 3; cpl = env->hflags & HF_CPL_MASK; /* check privilege if software int */ if (is_int && dpl < cpl) { - raise_exception_err(env, EXCP0D_GPF, intno * 8 + 2); + raise_exception_err(env, EXCP0D_GPF, (intno * 8) | 2 | ext); } /* check valid bit */ if (!(e2 & DESC_P_MASK)) { - raise_exception_err(env, EXCP0B_NOSEG, intno * 8 + 2); + raise_exception_err(env, EXCP0B_NOSEG, (intno * 8) | 2 | ext); } selector = e1 >> 16; offset = ((target_ulong)e3 << 32) | (e2 & 0xffff0000) | (e1 & 0x0000ffff); ist = e2 & 7; if ((selector & 0xfffc) == 0) { - raise_exception_err(env, EXCP0D_GPF, 0); + raise_exception_err(env, EXCP0D_GPF, ext); } if (load_segment(env, &e1, &e2, selector) != 0) { - raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); + raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext); } if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK))) { - raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); + raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext); } dpl = (e2 >> DESC_DPL_SHIFT) & 3; if (dpl > cpl) { - raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); + raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext); } if (!(e2 & DESC_P_MASK)) { - raise_exception_err(env, EXCP0B_NOSEG, selector & 0xfffc); + raise_exception_err(env, EXCP0B_NOSEG, (selector & 0xfffc) | ext); } if (!(e2 & DESC_L_MASK) || (e2 & DESC_B_MASK)) { - raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); + raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext); } set_seg_accessed(env, selector, &e2, 0); if (e2 & DESC_C_MASK) { @@ -1051,11 +1079,11 @@ static void do_interrupt64(CPUX86State *env, int intno, int is_int, if (dpl < cpl || ist != 0) { /* to inner privilege */ new_stack = 1; - sa.sp = get_rsp_from_tss(env, ist != 0 ? ist + 3 : dpl); + sa.sp = get_rsp_from_tss(env, ist != 0 ? ist + 3 : dpl, ext); } else { /* to same privilege */ if (env->eflags & VM_MASK) { - raise_exception_err(env, EXCP0D_GPF, selector & 0xfffc); + raise_exception_err(env, EXCP0D_GPF, (selector & 0xfffc) | ext); } new_stack = 0; sa.sp = env->regs[R_ESP]; @@ -1571,7 +1599,7 @@ void HELPER(ljmp_protected)(CPUX86State *env, int new_cs, target_ulong new_eip, raise_exception_err_ra(env, EXCP0D_GPF, new_cs & 0xfffc, GETPC()); } switch_tss_ra(env, new_cs, e1, e2, SWITCH_TSS_JMP, next_eip, - false, 0, GETPC()); + false, 0, GETPC(), 0); break; case 4: /* 286 call gate */ case 12: /* 386 call gate */ @@ -1791,7 +1819,7 @@ void HELPER(lcall_protected)(CPUX86State *env, int new_cs, target_ulong new_eip, raise_exception_err_ra(env, EXCP0D_GPF, new_cs & 0xfffc, GETPC()); } switch_tss_ra(env, new_cs, e1, e2, SWITCH_TSS_CALL, next_eip, - false, 0, GETPC()); + false, 0, GETPC(), 0); return; case 4: /* 286 call gate */ case 12: /* 386 call gate */ @@ -1864,7 +1892,7 @@ void HELPER(lcall_protected)(CPUX86State *env, int new_cs, target_ulong new_eip, if (shift == 2) { ss = dpl; /* SS = NULL selector with RPL = new CPL */ new_stack = 1; - sa.sp = get_rsp_from_tss(env, dpl); + sa.sp = get_rsp_from_tss(env, dpl, 0); sa.sp_mask = -1; sa.ss_base = 0; /* SS base is always zero in IA-32e mode */ LOG_PCALL("new ss:rsp=%04x:%016llx env->regs[R_ESP]=" @@ -1873,7 +1901,7 @@ void HELPER(lcall_protected)(CPUX86State *env, int new_cs, target_ulong new_eip, #endif { uint32_t sp32; - get_ss_esp_from_tss(env, &ss, &sp32, dpl, GETPC()); + get_ss_esp_from_tss(env, &ss, &sp32, dpl, GETPC(), 0); LOG_PCALL("new ss:esp=%04x:%08x param_count=%d env->regs[R_ESP]=" TARGET_FMT_lx "\n", ss, sp32, param_count, env->regs[R_ESP]); @@ -2313,7 +2341,7 @@ void HELPER(iret_protected)(CPUX86State *env, int shift, int next_eip) raise_exception_err_ra(env, EXCP0A_TSS, tss_selector & 0xfffc, GETPC()); } switch_tss_ra(env, tss_selector, e1, e2, SWITCH_TSS_IRET, next_eip, - false, 0, GETPC()); + false, 0, GETPC(), 0); } else { helper_ret_protected(env, shift, 1, 0, GETPC()); } -- 2.51.0.windows.2