From nobody Wed Oct 23 18:05:26 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XYcQt4vlbz5ZW47; Wed, 23 Oct 2024 18:05:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XYcQt4JyWz57KR; Wed, 23 Oct 2024 18:05:26 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729706726; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=se4nY/tZ+YpQAmn0jbcDbWoW+9f9vhQfV/U2ThY5O5E=; b=j/zfKLy/2DLGvPvm5OwgLs1OvruEj2VZEfFqDuGpoPb5kJ/Sq4Cq8wh74KUETA6iH+6myB 0nt7TDMYJnjE6NXNryku+kfQ99yBF6HFDHMpD4mTORHEyYi4BWAKpvYduK+dQStKP0g25f l94n0R1McSTqS8IGkGOKo/3gzECKB1C0PfKYJnKwGcLn9NABBcXHhfqPjj/hvcgXeS2F9r 8/IMH2XWnMGVdo7bsuU8fgmvwls7dy7sDiywLymZptjwWsZklOyBqLVBqFxHHvDPVCYSs/ dEAlVZCiC3JK80R9wJHFXj78pzjJfQwrIdotuRp33kMU5egM2mNZhxrxPMWPDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729706726; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=se4nY/tZ+YpQAmn0jbcDbWoW+9f9vhQfV/U2ThY5O5E=; b=Yc4SfP9Z8x7qoy7XBvHyLssNQ8ZnMfVWBAVYohvdDVQ4mZKPrTdqZcOufGuJoLkj5iwbLM mwtT5xPBiqs1dKcQOV0MEa7/dQBpnaa6NxZkpw3+p7T2vsBN+TBG8LdhxjxLghz7kgfDnB Xg2xbRcNv5SIgW0354KMHGg5y0MzHvxRuTDFZbmoz/OzwGeKCdM+3mvfQ+AIVV+6SehDyJ xjTfXSFN0vT5bJKVnfwtDw4cvrrC9fsUdH8csbXsz6HemF7oimI9g98e5nEsiclOgaWoIT IIJl9nYNYE21kgbflbeB182mptCDWc37f+Tm17qGIKJVYpH3X3ew0YS4bg5fwQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1729706726; a=rsa-sha256; cv=none; b=YluUaOrkH7HjTj7XEKLk9VnCqlDeZQTY3L7eaG5353U76uR/e9QGM03axZRWVLYAocS5vK NRXLEGYWmRugiHfbUv9BWBXMakxFJBYXox09EXT7WaFiOGC1gcq/U78M4SvdOLzChh6jD4 dC9oN1p+/e6IJvrONoiHwnh/Mm9HtINJ2iz0LQ0nP4wNqmRLFNoWm4tf4ntkR6kz42X8XU UjDGHYe9uAh2xvaqc+NVg/LgRCbUF/44twofImf4fJsq2vx6dwZI2J71xDiaqosV0C2ykT i6CVKDiM0oU35918kBPz1+Vllg07dA9dnFKzuNZNX+Xu8dkDV+Tg7+Pg7JZnIg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XYcQt3wHxz17ww; Wed, 23 Oct 2024 18:05:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 49NI5QbO022241; Wed, 23 Oct 2024 18:05:26 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49NI5QLY022238; Wed, 23 Oct 2024 18:05:26 GMT (envelope-from git) Date: Wed, 23 Oct 2024 18:05:26 GMT Message-Id: <202410231805.49NI5QLY022238@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 5ae76ff5138e - stable/14 - la57: explain how the trampoline works List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 5ae76ff5138e2ecbd2d740d75337d930360a6c6b Auto-Submitted: auto-generated The branch stable/14 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=5ae76ff5138e2ecbd2d740d75337d930360a6c6b commit 5ae76ff5138e2ecbd2d740d75337d930360a6c6b Author: Konstantin Belousov AuthorDate: 2024-10-20 15:47:50 +0000 Commit: Konstantin Belousov CommitDate: 2024-10-23 18:04:58 +0000 la57: explain how the trampoline works (cherry picked from commit 6244b9dc4a03e87246bad1c94067dfe54ff4cdbc) --- sys/amd64/amd64/locore.S | 61 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/sys/amd64/amd64/locore.S b/sys/amd64/amd64/locore.S index 1ed9085e655c..d39503adadf0 100644 --- a/sys/amd64/amd64/locore.S +++ b/sys/amd64/amd64/locore.S @@ -89,15 +89,36 @@ ENTRY(btext) 0: hlt jmp 0b -/* la57_trampoline(%rdi pml5) */ +/* + * void la57_trampoline(%rdi pml5) + * + * Entered in 4-level paging long mode on AP, hopefully returns alive in + * 5-level paging mode. The parameter is a pointer to a 5-level page + * table root. The passed 5-level page table, and the current 4-level page + * table, both must map the trampoline code page 1:1 physical, below 4G. + * The trampoline must be PIC because it is copied from kernel text into + * this page. + * + * The current paging level cannot be changed while paging is enabled, and + * paging cannot be disabled while in long mode. As consequence, code + * switches into the compat mode, then disables paging to descend into + * protected mode. There, the paging level bit CR4.LA57 can be changed, + * and code directly jumps back into long mode. + * + * Falling into the protected mode requires single-purpose GDT entries, + * which are provided by the private GDT. It is the caller's responsibility + * to + * - restore the GDT and %gsbase after the call + * - reset IDT back to long mode. + */ ENTRY(la57_trampoline) - movq %rsp,lst(%rip) - movq %rbx,lst+8(%rip) - movq %rbp,lst+0x10(%rip) + movq %rsp,lst(%rip) /* save registers into memeory */ + movq %rbx,lst+8(%rip) /* upper halves are not saved .. */ + movq %rbp,lst+0x10(%rip) /* by 64->32->64 switch */ movq %cr4,%rax - orq $CR4_LA57,%rax + orq $CR4_LA57,%rax /* 5-lvl %cr4 */ movq %rax,lst+0x18(%rip) - leaq la57_trampoline_end(%rip),%rsp + leaq la57_trampoline_end(%rip),%rsp /* priv stack */ movq %cr0,%rbp lgdtq la57_trampoline_gdt_desc(%rip) @@ -111,45 +132,45 @@ ENTRY(la57_trampoline) .code32 l1: movl $(3<<3),%eax - movl %eax,%ss + movl %eax,%ss /* 32bit paged, priv gdt and stack */ movl %cr4,%eax - andl $~(CR4_PGE | CR4_PCIDE),%eax + andl $~(CR4_PGE | CR4_PCIDE),%eax /* clear sensitive paging ctrls */ movl %eax,%cr4 movl %ebp,%eax - andl $~CR0_PG,%eax + andl $~CR0_PG,%eax /* protected mode */ movl %eax,%cr0 - movl $MSR_EFER,%ecx - rdmsr + movl $MSR_EFER,%ecx /* disable long mode bit */ + rdmsr /* to safer tweaking LA57 */ andl $~EFER_LME,%eax wrmsr - movl %cr4,%eax + movl %cr4,%eax /* finally safe to switch bit */ orl $CR4_LA57,%eax movl %eax,%cr4 - movl %edi,%cr3 + movl %edi,%cr3 /* and load the 5-level pgtable root */ rdmsr orl $EFER_LME,%eax - wrmsr + wrmsr /* prepare for ... */ - movl %ebp,%cr0 - jmp 1f + movl %ebp,%cr0 /* and jump back directly into long */ + jmp 1f /* mode from protected by enabling pg */ -1: pushl $(1<<3) +1: pushl $(1<<3) /* reload %cs */ pushl %ebx lretl .code64 -l2: movq lst(%rip),%rsp +l2: movq lst(%rip),%rsp /* back on C stack */ movq lst+8(%rip),%rbx movq lst+0x10(%rip),%rbp movq lst+0x18(%rip),%rax - movq %rax,%cr4 - retq + movq %rax,%cr4 /* re-enable paging controls */ + retq /* back to C */ .p2align 4,0 lst: .quad 0,0,0,0 ENTRY(la57_trampoline_gdt_desc)