Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2D5BCC433F5 for ; Fri, 18 Mar 2022 07:41:57 +0000 (UTC) Received: from localhost ([::1] helo=shelob.surriel.com) by shelob.surriel.com with esmtp (Exim 4.94.2) (envelope-from ) id 1nV7Ex-000486-AN; Fri, 18 Mar 2022 03:41:39 -0400 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]) by shelob.surriel.com with esmtps (TLS1.2) tls TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1nV7Eu-00047z-V7 for kernelnewbies@kernelnewbies.org; Fri, 18 Mar 2022 03:41:37 -0400 Received: by mail-pg1-x533.google.com with SMTP id q19so4508477pgm.6 for ; Fri, 18 Mar 2022 00:41:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to; bh=I/TQSb/JKovMRnxj8IaiHmNDa8iwdW9q37C2sjnoIOk=; b=o493t4mEYlGdmnviCoI9DFWPsEZ75NMZ0ivH52Wy5hVPaTfbupDp8L4AEtVvCN4BMZ dibgcQ6BbgZ/6gprF/GkurBowetHAa97F7i+hD2c7xa+jPY1pO6Lbsp5E4us4QUQlMfA +Z8rm15KoQAiQ0DaYinw+hRy725FE3I6fF0ak4xSjQl2ou6kbo3IdKJL5wNOc5s5rS6R GNzH95AJo7DGogs/AqVitXRgk68Tac6jjpz0BSb+GA1ilq2WuI1kd+mx00dswu0Y0bhK Cj7xDnEWkWWflTJTFQ3PBQ1nQQ7cF9CGqgUlplgggLKGwsOeQiHeasmYBE2N0XhTUXSC rKow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to; bh=I/TQSb/JKovMRnxj8IaiHmNDa8iwdW9q37C2sjnoIOk=; b=z9Ahv1SLTODqFcdAfDe9GXs9VBpR59nkHBaGHOsLaZcU81EP5d2uFJ5DDS8S7TW0uC A7AP3KK8Gc7GEGNDe1nydwUyqV3XauKyCUJ60kY555bKbNhyz24Pfg/aEv1/QwPd//SX FA/naBtmJF6cSKjOfZ7aEjzBb8szSH42y8/AZKOJHoKoGYfjwDMHtum14DsLw/a0IFUM fjvBypjvEzHPYq1xi3g/WPzQDOKVDyP2cTfwipsidyW2msHWozdUDreXnJOPyUdUgqyS DNK9BLW7WajMH7mKJxzTOGvB9qzGBDxQkBfzWeI1RKUOddUvtspy65z2Jvj1q2REeANX JSFA== X-Gm-Message-State: AOAM531ebdGBOe62kJbUtnX/o76ZTV+i78r+aUr69pvvoqxvy/EL3CSo XtAyLDp5qn9lJXMLyr8Ev41wDREuYJM= X-Google-Smtp-Source: ABdhPJzxZMi0AYBam4VT/LxdeCMX9Qol22o4lRVYFhp5jx1jbLulwKNfDrwZOmCRjhlQ821ChzTzYw== X-Received: by 2002:a05:6a00:198f:b0:4f6:c0e4:207d with SMTP id d15-20020a056a00198f00b004f6c0e4207dmr8297956pfl.82.1647589293255; Fri, 18 Mar 2022 00:41:33 -0700 (PDT) Received: from [183.99.9.117] ([183.99.9.117]) by smtp.gmail.com with ESMTPSA id h17-20020a63df51000000b0036b9776ae5bsm6719936pgj.85.2022.03.18.00.41.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 18 Mar 2022 00:41:32 -0700 (PDT) Message-ID: <6716e46d-412e-f9d6-b5d4-c704bbbb6a26@gmail.com> Date: Fri, 18 Mar 2022 16:41:29 +0900 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: Which Page table is appled when MMU is turned on in boot process of ARM64 Content-Language: en-US To: Chan Kim , kernelnewbies@kernelnewbies.org References: <7df5449d-9667-531e-c5fc-a008fc50c613@gmail.com> <0b3501d83a89$f04b8000$d0e28000$@etri.re.kr> From: JeongHwan Kim In-Reply-To: <0b3501d83a89$f04b8000$d0e28000$@etri.re.kr> X-BeenThere: kernelnewbies@kernelnewbies.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Learn about the Linux kernel List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: multipart/mixed; boundary="===============5542080551148428528==" Errors-To: kernelnewbies-bounces@kernelnewbies.org This is a multi-part message in MIME format. --===============5542080551148428528== Content-Type: multipart/alternative; boundary="------------ynY977tEluSedxh102ruTVKU" Content-Language: en-US This is a multi-part message in MIME format. --------------ynY977tEluSedxh102ruTVKU Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Thank you for reply. I have a question about the meaning of "Until MMU is turned on, the CPU has been using physical address and pc-relative jumps." I understand the CPU runs on physical address in assembly code before MMU is enabled. But how is it possible? For example, when the head.S is compiled, the PIC(position independent code) option is disabled as like this using -fno-PIE option: aarch64-linux-gnu-gcc -Wp,-MMD,arch/arm64/kernel/.head.o.d  -nostdinc -isystem /usr/lib/gcc-cross/aarch64-linux-gnu/9/include -I./arch/arm64/include -I./arch/arm64/include/generated  -I./include -I./arch/arm64/include/uapi -I./arch/arm6 4/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h -D__KERNEL__ -mlittle-endian -DKASAN_SHADOW_SCALE_SHIFT= -fmacro-prefix-map=./= -D__ASSEMB LY__ -fno-PIE -mabi=lp64 -fno-asynchronous-unwind-tables -fno-unwind-tables -DKASAN_SHADOW_SCALE_SHIFT= -Wa,-gdwarf-2    -c -o arch/arm64/kernel/head.o arch/arm64/kernel/head.S and system.map shows all the symbols of assembly code are virtual address. Please let me know the method how the assembly code runs in PIC way. Only are the pc-relative jumps needed? Regards, J.Hwan KIm On 22. 3. 18. 14:35, Chan Kim wrote: > Hi JeongHwan, > > Recently I followed the kernel boot process so now I understand how it's > doing. > The kernel starts at HEAD and it jumps to primary_entry. (code below is > 5.10.0. but in 5.4.21 it jumped to stext, just name change) > Primary_entry looks like this. > > SYM_CODE_START(primary_entry) > > bl preserve_boot_args > bl el2_setup // Drop to EL1, w0=cpu_boot_mode > adrp x23, __PHYS_OFFSET > and x23, x23, MIN_KIMG_ALIGN - 1 // KASLR offset, defaults to > 0 > bl set_cpu_boot_mode_flag > bl __create_page_tables > /* > * The following calls CPU setup code, see arch/arm64/mm/proc.S > for > * details. > * On return, the CPU will be ready for the MMU to be turned on > and > * the TCR will have been set. > */ > bl __cpu_setup // initialise processor > b __primary_switch > SYM_CODE_END(primary_entry) > > The page tables for id mapping (for 0x80000000 ~ .. physical address) and > swapper_pgdir (for virtual address) are setup inside __create_page_tables. > MMU is turned on in __primary_switch. (code below for 5.10.0-rc5) > You see there is bl __enable_mmu. Until MMU is turned on, the CPU has been > using physical address and pc-relative jumps. > At the instant MMU is turned on, the page table for the current physical > address range is already setup so there is no problem using physical > address.(just identical mapping) > The first place where kernel virtual address is used is at the end of > __primary_switch. The value __primary_switched is a virtual address > (starting with 0xffffffc0....) and from that point on virtual->physical > address mapping is used. Old version will be the same. (I checked > previously for 5.4.21) > > > SYM_FUNC_START_LOCAL(__primary_switch) > #ifdef CONFIG_RANDOMIZE_BASE > mov x19, x0 // preserve new SCTLR_EL1 value > mrs x20, sctlr_el1 // preserve old SCTLR_EL1 > value > #endif > > adrp x1, init_pg_dir > bl __enable_mmu > #ifdef CONFIG_RELOCATABLE > #ifdef CONFIG_RELR > mov x24, #0 // no RELR displacement yet > #endif > bl __relocate_kernel > #ifdef CONFIG_RANDOMIZE_BASE > ldr x8, =__primary_switched > adrp x0, __PHYS_OFFSET > blr x8 > > /* > * If we return here, we have a KASLR displacement in > x23 which we need > * to take into account by discarding the current kernel > mapping and > * creating a new one. > */ > pre_disable_mmu_workaround > msr sctlr_el1, x20 // disable the MMU > isb > bl __create_page_tables // recreate kernel > mapping > > tlbi vmalle1 // Remove any stale TLB > entries > dsb nsh > > msr sctlr_el1, x19 // re-enable the MMU > isb > ic iallu // flush instructions fetched > dsb nsh // via old mapping > isb > > bl __relocate_kernel > #endif > #endif > ldr x8, =__primary_switched > adrp x0, __PHYS_OFFSET > br x8 > SYM_FUNC_END(__primary_switch) > > > Hope this helps. > Have a nice day! > > Chan > >> -----Original Message----- >> From: JeongHwan Kim >> Sent: Friday, March 18, 2022 10:41 AM >> To:kernelnewbies@kernelnewbies.org >> Subject: Which Page table is appled when MMU is turned on in boot process >> of ARM64 >> >> Hi, everyone >> >> >> When ARM64 boots, it setup 2 page tables ("idmap_pg_dir" and >> "swapper_pgdir"). >> >> On turning on MMU, which page table is applied? >> >> I heard that "idmap_pg_dir" is applied as soon as MMU is turned on. >> >> I cannot understand why "idmap_pg_dir" is applied, which is for "Lower" >> address. >> >> As I understand, the virtual address of kernel code is in "Higher" > address. >> Can you explain why "idmap_pg_dir" is applied on MMU turning on? >> >> >> Thanks in advance >> >> J.Hwan Kim >> >> >> >> >> _______________________________________________ >> Kernelnewbies mailing list >> Kernelnewbies@kernelnewbies.org >> https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies > > > --------------ynY977tEluSedxh102ruTVKU Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

Thank you for reply.


I have a question about the meaning of "Until MMU is turned on, the CPU has been

using physical address and pc-relative jumps."

I understand the CPU runs on physical address in assembly code before MMU is enabled.

But how is it possible?

For example, when the head.S is compiled, the PIC(position independent code) option is disabled as like this using -fno-PIE option:

aarch64-linux-gnu-gcc -Wp,-MMD,arch/arm64/kernel/.head.o.d  -nostdinc -isystem /usr/lib/gcc-cross/aarch64-linux-gnu/9/include -I./arch/arm64/include -I./arch/arm64/include/generated  -I./include -I./arch/arm64/include/uapi -I./arch/arm6
4/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h -D__KERNEL__ -mlittle-endian -DKASAN_SHADOW_SCALE_SHIFT= -fmacro-prefix-map=./= -D__ASSEMB
LY__ -fno-PIE -mabi=lp64 -fno-asynchronous-unwind-tables -fno-unwind-tables -DKASAN_SHADOW_SCALE_SHIFT= -Wa,-gdwarf-2    -c -o arch/arm64/kernel/head.o arch/arm64/kernel/head.S

and system.map shows all the symbols of assembly code are virtual address. 

Please let me know the method how the assembly code runs in PIC way.
Only are the pc-relative jumps needed?

Regards,
J.Hwan KIm 

On 22. 3. 18. 14:35, Chan Kim wrote:
Hi JeongHwan,

Recently I followed the kernel boot process so now I understand how it's
doing.
The kernel starts at HEAD and it jumps to primary_entry. (code below is
5.10.0.  but in 5.4.21 it jumped to stext, just name change)
Primary_entry looks like this.

       SYM_CODE_START(primary_entry)

            bl  preserve_boot_args
            bl  el2_setup           // Drop to EL1, w0=cpu_boot_mode
            adrp    x23, __PHYS_OFFSET
            and x23, x23, MIN_KIMG_ALIGN - 1    // KASLR offset, defaults to
0
            bl  set_cpu_boot_mode_flag
            bl  __create_page_tables
            /*
             * The following calls CPU setup code, see arch/arm64/mm/proc.S
for
             * details.
             * On return, the CPU will be ready for the MMU to be turned on
and
             * the TCR will have been set.
             */
            bl  __cpu_setup         // initialise processor
            b   __primary_switch
        SYM_CODE_END(primary_entry)

The page tables for id mapping (for 0x80000000 ~ .. physical address) and
swapper_pgdir (for virtual address) are setup inside __create_page_tables.
MMU is turned on in __primary_switch. (code below for 5.10.0-rc5)
You see there is bl __enable_mmu. Until MMU is turned on, the CPU has been
using physical address and pc-relative jumps. 
At the instant MMU is turned on, the page table for the current physical
address range is already setup so there is no problem using physical
address.(just identical mapping) 
The first place where kernel virtual address is used is at the end of
__primary_switch. The value __primary_switched is a virtual address
(starting with 0xffffffc0....) and from that point on virtual->physical
address mapping is used.  Old version will be the same. (I checked
previously for 5.4.21)


		SYM_FUNC_START_LOCAL(__primary_switch)
		#ifdef CONFIG_RANDOMIZE_BASE
		    mov x19, x0             // preserve new SCTLR_EL1 value
		    mrs x20, sctlr_el1          // preserve old SCTLR_EL1
value
		#endif
		
		    adrp    x1, init_pg_dir
		    bl  __enable_mmu
		#ifdef CONFIG_RELOCATABLE
		#ifdef CONFIG_RELR
		    mov x24, #0             // no RELR displacement yet
		#endif
		    bl  __relocate_kernel
		#ifdef CONFIG_RANDOMIZE_BASE
		    ldr x8, =__primary_switched
		    adrp    x0, __PHYS_OFFSET
		    blr x8
		
		    /*
		     * If we return here, we have a KASLR displacement in
x23 which we need
		     * to take into account by discarding the current kernel
mapping and
		     * creating a new one.
		     */
		    pre_disable_mmu_workaround
		    msr sctlr_el1, x20          // disable the MMU
		    isb
		    bl  __create_page_tables        // recreate kernel
mapping
		
		    tlbi    vmalle1             // Remove any stale TLB
entries
		    dsb nsh
		
		    msr sctlr_el1, x19          // re-enable the MMU
		    isb
		    ic  iallu               // flush instructions fetched
		    dsb nsh             // via old mapping
		    isb
		
		    bl  __relocate_kernel
		#endif
		#endif
		    ldr x8, =__primary_switched
		    adrp    x0, __PHYS_OFFSET
		    br  x8
		SYM_FUNC_END(__primary_switch)
		

Hope this helps.
Have a nice day!

Chan

-----Original Message-----
From: JeongHwan Kim <frog007.kernel.kr@gmail.com>
Sent: Friday, March 18, 2022 10:41 AM
To: kernelnewbies@kernelnewbies.org
Subject: Which Page table is appled when MMU is turned on in boot process
of ARM64

Hi, everyone


When ARM64 boots, it setup 2 page tables ("idmap_pg_dir" and
"swapper_pgdir").

On turning on MMU, which page table is applied?

I heard that "idmap_pg_dir" is applied as soon as MMU is turned on.

I cannot understand why "idmap_pg_dir" is applied, which is for "Lower"
address.

As I understand, the virtual address of kernel code is in "Higher"
address.
Can you explain why "idmap_pg_dir" is applied on MMU turning on?


Thanks in advance

J.Hwan Kim




_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies



--------------ynY977tEluSedxh102ruTVKU-- --===============5542080551148428528== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies --===============5542080551148428528==--