From nobody Sun Oct 20 17:00:46 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 4XWl7f5vYYz5ZWyZ; Sun, 20 Oct 2024 17:00:46 +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 4XWl7f5LRTz4Ffv; Sun, 20 Oct 2024 17:00:46 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729443646; 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=JJkU3JA/sfUdyWVuXrHtyWD5nsTqraVK80nNt3pjiTo=; b=yjiwaC7RY2+5hfL64x+5lbHI0P5rx47x2TLhyqTN/IqIedqQiwmrv5iqhWElvBMvOgfnt0 8npNGB1QjaQXbd3mAIUzSr3PynbdX+XEUrMgwA0r0OKfrmDGbwGRMf6tQxkrKPym7IwMLf JNM9WO7/zHhe1LeAaSAwZf2yyd5J0OdGTwnTuzktV7aygqZgRBmV6S13yC1BJ4pNQFJPKD jlvT2R8IqA7Pb0edOq7Rk9w7G8NbmaZyiBisqjCOAhdNt7SXnqMyW7+EIeiqUPrYkzPzao av6IIrHzWC9juZAcZndCxl5e1uGJyNuc0hQG+Zj3KgS1dmP1CD7KwRhWVvP/lA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729443646; 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=JJkU3JA/sfUdyWVuXrHtyWD5nsTqraVK80nNt3pjiTo=; b=rGAzBvfyAAhyQNgPyEDgssCsxAdg1RKdcZSyxpkXE+Me5hRuaHpNc2UGoobHVzb4rDvKH3 hagY2L7TZE7pa1ypBeKWgmNaUNu2nHrdvRTpStElXDrpqlaYgzY3728loFIFKaCeLmDnsQ uvh5fr4GDYaOPYdiSwUoA46nuKeq+pDYdZ7nujIurTK+8yVXqxlEFjbhZWnrzC/FGqK6bi 2ulU94iOafD2ZdjJ3fqVm60eAUCnI6uY9JM5Pk0oW6mvuphavxwsQeHS8LzP9llxaAIVmW CuesyxD9X9ZzkO310pLf8QwEeq6kOZt2cXody5WtL9n6SOBEsmu3dT/So8QYRQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1729443646; a=rsa-sha256; cv=none; b=E4HWYOs05o5klsgOvgs1B1Sbd6HD6FFVtPwVlzP4KXTIJ59jB890OsPUtx0GmKXPcHD/ea P8AKAmqRABHGs1c9a4UkTVGn9zsbUbP+daDgqXhjg/rR6MrlLPseHEeGWcKAFYtIol//ac MMH9f2XdilecMklYwODLPItvWLRPx/6ZpjWapzcu8K4RnHXIPCWXhKNL4V6yqdTu24/AQF sMqgw8ptIB7tMpXDi2wiN8xbJ9bqOuPXAu/GjMLVJtfN3kMFWhByN3qlCtB3kN8AdIgdmG iKf3lxTiEg79O1Nws4qC+VtspPYqdhhrJkNsH47xXLcWpJSUYb1e7w4brJig9g== 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 4XWl7f4qtlz18H7; Sun, 20 Oct 2024 17:00:46 +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 49KH0kVU088505; Sun, 20 Oct 2024 17:00:46 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49KH0kDb088502; Sun, 20 Oct 2024 17:00:46 GMT (envelope-from git) Date: Sun, 20 Oct 2024 17:00:46 GMT Message-Id: <202410201700.49KH0kDb088502@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 6244b9dc4a03 - main - 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/main X-Git-Reftype: branch X-Git-Commit: 6244b9dc4a03e87246bad1c94067dfe54ff4cdbc Auto-Submitted: auto-generated The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=6244b9dc4a03e87246bad1c94067dfe54ff4cdbc commit 6244b9dc4a03e87246bad1c94067dfe54ff4cdbc Author: Konstantin Belousov AuthorDate: 2024-10-20 15:47:50 +0000 Commit: Konstantin Belousov CommitDate: 2024-10-20 17:00:23 +0000 la57: explain how the trampoline works Reviewed by: markj, imp (previous version) Sponsored by: The FreeBSD Foundation MFC after: 3 days Differential revision: https://reviews.freebsd.org/D47208 --- 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)