init/main.c

 941         set_task_stack_end_magic(&init_task);


kernel/fork.c

 951 void set_task_stack_end_magic(struct task_struct *tsk)
 952 {
 953         unsigned long *stackend;
 954
 955         stackend = end_of_stack(tsk);
 956         *stackend = STACK_END_MAGIC;    /* for overflow detection */

stack 끝에 STACK_END_MAGIC 코드 넣어서 stack 넘침 여부 확인


 957 }


init/init_task.c

 60 /*
 61  * Set up the first task table, touch at your own risk!. Base=0,
 62  * limit=0x1fffff (=2MB)
 63  */
 64 struct task_struct init_task
 65 #ifdef CONFIG_ARCH_TASK_STRUCT_ON_STACK
 66         __init_task_data

__init_task_data : CONFIG_ARCH_TASK_STRUCT_ON_STACK 활성화된 경우

                             init_task의 위치를 ".data..init_task" section 으로

 

 67 #endif
 68         __aligned(L1_CACHE_BYTES)
 69 = {
 70 #ifdef CONFIG_THREAD_INFO_IN_TASK
 71         .thread_info    = INIT_THREAD_INFO(init_task),
 72         .stack_refcount = REFCOUNT_INIT(1),
 73 #endif
 74         .__state        = 0,
 75         .stack          = init_stack,
 76         .usage          = REFCOUNT_INIT(2),
 77         .flags          = PF_KTHREAD,
 78         .prio           = MAX_PRIO - 20,
 79         .static_prio    = MAX_PRIO - 20,
 80         .normal_prio    = MAX_PRIO - 20,
 81         .policy         = SCHED_NORMAL,
 82         .cpus_ptr       = &init_task.cpus_mask,
 83         .user_cpus_ptr  = NULL,
 84         .cpus_mask      = CPU_MASK_ALL,
 85         .nr_cpus_allowed= NR_CPUS,
 86         .mm             = NULL,
 87         .active_mm      = &init_mm,
 88         .restart_block  = {
 89                 .fn = do_no_restart_syscall,
 90         },
 91         .se             = {
 92                 .group_node     = LIST_HEAD_INIT(init_task.se.group_node),
 93         },
 94         .rt             = {
 95                 .run_list       = LIST_HEAD_INIT(init_task.rt.run_list),
 96                 .time_slice     = RR_TIMESLICE,
 97         },
 98         .tasks          = LIST_HEAD_INIT(init_task.tasks),

...

213 };
214 EXPORT_SYMBOL(init_task);

 


 26 static inline unsigned long *end_of_stack(const struct task_struct *task)
 27 {
 28 #ifdef CONFIG_STACK_GROWSUP
 29     return (unsigned long *)((unsigned long)task->stack + THREAD_SIZE) - 1;

stack 포인터가 아래에서 위로 증가 하는 경우 최상위 주소 반환


 30 #else
 31     return task->stack;
 32 #endif
 33 }

 


420 SYM_FUNC_START_LOCAL(__primary_switched)
421         adr_l   x4, init_task
422         init_cpu_task x4, x5, x6

 

397         .macro  init_cpu_task tsk, tmp1, tmp2
398         msr     sp_el0, \tsk

399
400         ldr     \tmp1, [\tsk, #TSK_STACK]
401         add     sp, \tmp1, #THREAD_SIZE
402         sub     sp, sp, #PT_REGS_SIZE

 

 

'linux' 카테고리의 다른 글

6.1/Head.S  (0) 2023.11.16
6.1/kaslr_requires_kpti(void)  (0) 2023.11.10
6.1/smp_setup_processor_id()  (0) 2023.02.23
Linux Kernel 6.1/start_kernel(void)  (0) 2023.02.09
Linux kernel 5.0 ARM64 Compile  (0) 2019.03.12

init/main.c

 

 936 asmlinkage __visible void __init __no_sanitize_address start_kernel(void)

asmlinkage : 어셈블리어로 작성된 코드에 호출될수 있음

__visible     : gdb 등 외부 프로그램에서 접근 할수 있음

https://gcc.gnu.org/onlinedocs/gcc-8.1.0/gcc/Common-Function-Attributes.html#Common-Function-Attributes

__init           : .init.text section에 할당

__no_sanitize_address : The no_sanitize_address attribute on functions is used to inform the compiler that it should not instrument memory accesses in the function when compiling with the -fsanitize=address option. The no_address_safety_analysis is a deprecated alias of the no_sanitize_address attribute, new code should use no_sanitize_address.

 

 937 {
 938         char *command_line;
 939         char *after_dashes;
 940
 941         set_task_stack_end_magic(&init_task);
 942         smp_setup_processor_id();
 943         debug_objects_early_init();
 944         init_vmlinux_build_id();
 945
 946         cgroup_init_early();
 947
 948         local_irq_disable();
 949         early_boot_irqs_disabled = true;
 950
 951         /*
 952          * Interrupts are still disabled. Do necessary setups, then
 953          * enable them.
 954          */
 955         boot_cpu_init();
 956         page_address_init();
 957         pr_notice("%s", linux_banner);
 958         early_security_init();
 959         setup_arch(&command_line);
 960         setup_boot_config();
 961         setup_command_line(command_line);
 962         setup_nr_cpu_ids();
 963         setup_per_cpu_areas();
 964         smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
 965         boot_cpu_hotplug_init();
 966
 967         build_all_zonelists(NULL);
 968         page_alloc_init();
 969
 970         pr_notice("Kernel command line: %s\n", saved_command_line);
 971         /* parameters may set static keys */
 972         jump_label_init();
 973         parse_early_param();

 974         after_dashes = parse_args("Booting kernel",
 975                                   static_command_line, __start___param,
 976                                   __stop___param - __start___param,
 977                                   -1, -1, NULL, &unknown_bootoption);
 978         print_unknown_bootoptions();
 979         if (!IS_ERR_OR_NULL(after_dashes))
 980                 parse_args("Setting init args", after_dashes, NULL, 0, -1, -1,
 981                            NULL, set_init_arg);
 982         if (extra_init_args)
 983                 parse_args("Setting extra init args", extra_init_args,
 984                            NULL, 0, -1, -1, NULL, set_init_arg);
 985
 986         /* Architectural and non-timekeeping rng init, before allocator init */
 987         random_init_early(command_line);
 988
 989         /*
 990          * These use large bootmem allocations and must precede
 991          * kmem_cache_init()
 992          */
 993         setup_log_buf(0);
 994         vfs_caches_init_early();
 995         sort_main_extable();
 996         trap_init();
 997         mm_init();
 998
 999         ftrace_init();
1000
1001         /* trace_printk can be enabled here */
1002         early_trace_init();
1003
1004         /*
1005          * Set up the scheduler prior starting any interrupts (such as the
1006          * timer interrupt). Full topology setup happens at smp_init()
1007          * time - but meanwhile we still have a functioning scheduler.
1008          */
1009         sched_init();
1010
1011         if (WARN(!irqs_disabled(),
1012                  "Interrupts were enabled *very* early, fixing it\n"))
1013                 local_irq_disable();
1014         radix_tree_init();
1015         maple_tree_init();

1016
1017         /*
1018          * Set up housekeeping before setting up workqueues to allow the unbound
1019          * workqueue to take non-housekeeping into account.
1020          */
1021         housekeeping_init();
1022
1023         /*
1024          * Allow workqueue creation and work item queueing/cancelling
1025          * early.  Work item execution depends on kthreads and starts after
1026          * workqueue_init().
1027          */
1028         workqueue_init_early();
1029
1030         rcu_init();
1031
1032         /* Trace events are available after this */
1033         trace_init();
1034
1035         if (initcall_debug)
1036                 initcall_debug_enable();
1037
1038         context_tracking_init();
1039         /* init some links before init_ISA_irqs() */
1040         early_irq_init();
1041         init_IRQ();
1042         tick_init();
1043         rcu_init_nohz();
1044         init_timers();
1045         srcu_init();
1046         hrtimers_init();
1047         softirq_init();
1048         timekeeping_init();
1049         time_init();
1050
1051         /* This must be after timekeeping is initialized */
1052         random_init();
1053
1054         /* These make use of the fully initialized rng */
1055         kfence_init();
1056         boot_init_stack_canary();

1057
1058         perf_event_init();
1059         profile_init();
1060         call_function_init();
1061         WARN(!irqs_disabled(), "Interrupts were enabled early\n");
1062
1063         early_boot_irqs_disabled = false;
1064         local_irq_enable();
1065
1066         kmem_cache_init_late();
1067
1068         /*
1069          * HACK ALERT! This is early. We're enabling the console before
1070          * we've done PCI setups etc, and console_init() must be aware of
1071          * this. But we do want output early, in case something goes wrong.
1072          */
1073         console_init();
1074         if (panic_later)
1075                 panic("Too many boot %s vars at `%s'", panic_later,
1076                       panic_param);
1077
1078         lockdep_init();
1079
1080         /*
1081          * Need to run this when irqs are enabled, because it wants
1082          * to self-test [hard/soft]-irqs on/off lock inversion bugs
1083          * too:
1084          */
1085         locking_selftest();
1086
1087         /*
1088          * This needs to be called before any devices perform DMA
1089          * operations that might use the SWIOTLB bounce buffers. It will
1090          * mark the bounce buffers as decrypted so that their usage will
1091          * not cause "plain-text" data to be decrypted when accessed.
1092          */
1093         mem_encrypt_init();
1094
1095 #ifdef CONFIG_BLK_DEV_INITRD
1096         if (initrd_start && !initrd_below_start_ok &&
1097             page_to_pfn(virt_to_page((void *)initrd_start)) < min_low_pfn) {
1098                 pr_crit("initrd overwritten (0x%08lx < 0x%08lx) - disabling it.\n",
1099                     page_to_pfn(virt_to_page((void *)initrd_start)),
1100                     min_low_pfn);
1101                 initrd_start = 0;
1102         }
1103 #endif

1104         setup_per_cpu_pageset();
1105         numa_policy_init();
1106         acpi_early_init();
1107         if (late_time_init)
1108                 late_time_init();
1109         sched_clock_init();
1110         calibrate_delay();
1111         pid_idr_init();
1112         anon_vma_init();
1113 #ifdef CONFIG_X86
1114         if (efi_enabled(EFI_RUNTIME_SERVICES))
1115                 efi_enter_virtual_mode();
1116 #endif
1117         thread_stack_cache_init();
1118         cred_init();
1119         fork_init();
1120         proc_caches_init();
1121         uts_ns_init();
1122         key_init();
1123         security_init();
1124         dbg_late_init();
1125         net_ns_init();
1126         vfs_caches_init();
1127         pagecache_init();
1128         signals_init();
1129         seq_file_init();
1130         proc_root_init();
1131         nsfs_init();
1132         cpuset_init();
1133         cgroup_init();
1134         taskstats_init_early();
1135         delayacct_init();
1136
1137         poking_init();
1138         check_bugs();
1139
1140         acpi_subsystem_init();
1141         arch_post_acpi_subsys_init();
1142         kcsan_init();
1143
1144         /* Do the rest non-__init'ed, we're now alive */
1145         arch_call_rest_init();
1146
1147         prevent_tail_call_optimization();
1148 }

 

'linux' 카테고리의 다른 글

6.1/Head.S  (0) 2023.11.16
6.1/kaslr_requires_kpti(void)  (0) 2023.11.10
6.1/smp_setup_processor_id()  (0) 2023.02.23
6.1/set_task_stack_end_magic()  (0) 2023.02.09
Linux kernel 5.0 ARM64 Compile  (0) 2019.03.12

Linux kernel 5.0 ARM64 Compile

 

 

- OS : 우분투 18.04에서

 

 

1. 사전 준비

sudo apt-get install build-essential libncurses5 libncurses5-dev bin86 kernel-package libssl-dev bison flex libelf-dev

 

 

2. Cross-Compiler 설치

  • 64bit ARM, AARCH64 을 지원하는 gcc는 gcc-aarch64-linux-gun-*로 시작함
  • $sudo apt install binutils-aarch64-linux-gnu gcc-aarch64-linux-gnu g++-aarch64-linux-gnu



출처: https://ng1004.tistory.com/14 

 

 

 

3. 환경 구성

 

 

cat > arm64_env.sh

#!/bin/sh
export KERNEL=kernel7
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-

위 코드 복붙 후

 

source arm64_env.sh

 

 

4. Linux Kernel 소스 코드 받기

git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git

 

 

5. Compile

 

make bcmrpi3_defconfig

bcmrpi3_defconfig 없어서 에러나는 경우 라즈베리파이 소스에서 복사해오기

https://github.com/raspberrypi/linux/blob/rpi-5.15.y/arch/arm64/configs/bcm2711_defconfig

cp bcm2711_defconfig ~/linux_stable/arch/arm64/configs

wget https://raw.githubusercontent.com/raspberrypi/linux/rpi-5.15.y/arch/arm64/configs/bcm2711_defconfig

make bcm2711_defconfig
make menuconfig

make -j12 vmlinux

 

 

ps)

arm64 인 경우

make bcm2711_defconfig 대신 make defconfig 만으로되 된다고함.

https://linux-kernel-labs.github.io/refs/heads/master/labs/arm_kernel_development.html#compiling-the-linux-kernel-on-arm

 

 

 

=================

 

라즈베리파이용으로 Linux Kernel을 Compile 하고자 하는 경우

https://johnjarrer.tistory.com/70

 

디바이스 드라이버 / 실습 환경 설정 및 준비

디바이스 드라이버은 커널에 적재되기 때문에 디바이스 드라이버 코드를 컴파일하기 위해서는 커널 소스가 따로 필요하다. https://johnjarrer.tistory.com/110 디바이스 드라이버 / 커널 소스가 필요한

johnjarrer.tistory.com

 

 

 

 

'linux' 카테고리의 다른 글

6.1/Head.S  (0) 2023.11.16
6.1/kaslr_requires_kpti(void)  (0) 2023.11.10
6.1/smp_setup_processor_id()  (0) 2023.02.23
6.1/set_task_stack_end_magic()  (0) 2023.02.09
Linux Kernel 6.1/start_kernel(void)  (0) 2023.02.09

+ Recent posts