RHEL4/init/main.c
<<
>>
Prefs
   1/*
   2 *  linux/init/main.c
   3 *
   4 *  Copyright (C) 1991, 1992  Linus Torvalds
   5 *
   6 *  GK 2/5/95  -  Changed to support mounting root fs via NFS
   7 *  Added initrd & change_root: Werner Almesberger & Hans Lermen, Feb '96
   8 *  Moan early if gcc is old, avoiding bogus kernels - Paul Gortmaker, May '96
   9 *  Simplified starting of init:  Michael A. Griffith <grif@acm.org> 
  10 */
  11
  12#define __KERNEL_SYSCALLS__
  13
  14#include <linux/config.h>
  15#include <linux/types.h>
  16#include <linux/module.h>
  17#include <linux/proc_fs.h>
  18#include <linux/devfs_fs_kernel.h>
  19#include <linux/kernel.h>
  20#include <linux/syscalls.h>
  21#include <linux/string.h>
  22#include <linux/ctype.h>
  23#include <linux/delay.h>
  24#include <linux/utsname.h>
  25#include <linux/ioport.h>
  26#include <linux/init.h>
  27#include <linux/smp_lock.h>
  28#include <linux/initrd.h>
  29#include <linux/hdreg.h>
  30#include <linux/bootmem.h>
  31#include <linux/tty.h>
  32#include <linux/gfp.h>
  33#include <linux/percpu.h>
  34#include <linux/kmod.h>
  35#include <linux/kernel_stat.h>
  36#include <linux/security.h>
  37#include <linux/workqueue.h>
  38#include <linux/profile.h>
  39#include <linux/rcupdate.h>
  40#include <linux/moduleparam.h>
  41#include <linux/kallsyms.h>
  42#include <linux/writeback.h>
  43#include <linux/cpu.h>
  44#include <linux/efi.h>
  45#include <linux/unistd.h>
  46#include <linux/rmap.h>
  47#include <linux/mempolicy.h>
  48
  49#include <asm/io.h>
  50#include <asm/bugs.h>
  51#include <asm/setup.h>
  52#include <asm/timex.h>
  53
  54/*
  55 * This is one of the first .c files built. Error out early
  56 * if we have compiler trouble..
  57 */
  58#if __GNUC__ == 2 && __GNUC_MINOR__ == 96
  59#ifdef CONFIG_FRAME_POINTER
  60#error This compiler cannot compile correctly with frame pointers enabled
  61#endif
  62#endif
  63
  64#ifdef CONFIG_X86_LOCAL_APIC
  65#include <asm/smp.h>
  66#endif
  67
  68/*
  69 * Versions of gcc older than that listed below may actually compile
  70 * and link okay, but the end product can have subtle run time bugs.
  71 * To avoid associated bogus bug reports, we flatly refuse to compile
  72 * with a gcc that is known to be too old from the very beginning.
  73 */
  74#if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 95)
  75#error Sorry, your GCC is too old. It builds incorrect kernels.
  76#endif
  77
  78extern char *linux_banner;
  79
  80static int init(void *);
  81
  82extern void init_IRQ(void);
  83extern void sock_init(void);
  84extern void fork_init(unsigned long);
  85extern void mca_init(void);
  86extern void sbus_init(void);
  87extern void sysctl_init(void);
  88extern void signals_init(void);
  89extern void buffer_init(void);
  90extern void pidhash_init(void);
  91extern void pidmap_init(void);
  92extern void prio_tree_init(void);
  93extern void radix_tree_init(void);
  94extern void free_initmem(void);
  95extern void populate_rootfs(void);
  96extern void driver_init(void);
  97extern void prepare_namespace(void);
  98#ifdef  CONFIG_ACPI
  99extern void acpi_early_init(void);
 100#else
 101static inline void acpi_early_init(void) { }
 102#endif
 103
 104#ifdef CONFIG_TC
 105extern void tc_init(void);
 106#endif
 107
 108enum system_states system_state;
 109EXPORT_SYMBOL(system_state);
 110
 111/*
 112 * Boot command-line arguments
 113 */
 114#define MAX_INIT_ARGS 32
 115#define MAX_INIT_ENVS 32
 116
 117extern void time_init(void);
 118/* Default late time init is NULL. archs can override this later. */
 119void (*late_time_init)(void);
 120extern void softirq_init(void);
 121
 122/* Untouched command line (eg. for /proc) saved by arch-specific code. */
 123char saved_command_line[COMMAND_LINE_SIZE];
 124
 125static char *execute_command;
 126
 127/* Setup configured maximum number of CPUs to activate */
 128static unsigned int max_cpus = NR_CPUS;
 129
 130/*
 131 * Setup routine for controlling SMP activation
 132 *
 133 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
 134 * activation entirely (the MPS table probe still happens, though).
 135 *
 136 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
 137 * greater than 0, limits the maximum number of CPUs activated in
 138 * SMP mode to <NUM>.
 139 */
 140static int __init nosmp(char *str)
 141{
 142        max_cpus = 0;
 143        return 1;
 144}
 145
 146__setup("nosmp", nosmp);
 147
 148static int __init maxcpus(char *str)
 149{
 150        get_option(&str, &max_cpus);
 151        return 1;
 152}
 153
 154__setup("maxcpus=", maxcpus);
 155
 156static char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
 157char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
 158static const char *panic_later, *panic_param;
 159
 160static int __init obsolete_checksetup(char *line)
 161{
 162        struct obs_kernel_param *p;
 163        extern struct obs_kernel_param __setup_start, __setup_end;
 164
 165        p = &__setup_start;
 166        do {
 167                int n = strlen(p->str);
 168                if (!strncmp(line, p->str, n)) {
 169                        if (p->early) {
 170                                /* Already done in parse_early_param?  (Needs
 171                                 * exact match on param part) */
 172                                if (line[n] == '\0' || line[n] == '=')
 173                                        return 1;
 174                        } else if (!p->setup_func) {
 175                                printk(KERN_WARNING "Parameter %s is obsolete,"
 176                                       " ignored\n", p->str);
 177                                return 1;
 178                        } else if (p->setup_func(line + n))
 179                                return 1;
 180                }
 181                p++;
 182        } while (p < &__setup_end);
 183        return 0;
 184}
 185
 186static unsigned long preset_lpj;
 187static int __init lpj_setup(char *str)
 188{
 189        preset_lpj = simple_strtoul(str,NULL,0);
 190        return 1;
 191}
 192
 193__setup("lpj=", lpj_setup);
 194
 195/*
 196 * This should be approx 2 Bo*oMips to start (note initial shift), and will
 197 * still work even if initially too large, it will just take slightly longer
 198 */
 199unsigned long loops_per_jiffy = (1<<12);
 200
 201EXPORT_SYMBOL(loops_per_jiffy);
 202
 203#ifdef ARCH_HAS_READ_CURRENT_TIMER
 204
 205/* This routine uses the read_current_timer() routine and gets the
 206 * loops per jiffy directly, instead of guessing it using delay().
 207 * Also, this code tries to handle non-maskable asynchronous events
 208 * (like SMIs)
 209 */
 210#define DELAY_CALIBRATION_TICKS                 ((HZ < 100) ? 1 : (HZ/100))
 211#define MAX_DIRECT_CALIBRATION_RETRIES          5
 212
 213static unsigned long __devinit calibrate_delay_direct(void)
 214{
 215        unsigned long pre_start, start, post_start;
 216        unsigned long pre_end, end, post_end;
 217        unsigned long start_jiffies;
 218        unsigned long tsc_rate_min, tsc_rate_max;
 219        unsigned long good_tsc_sum = 0;
 220        unsigned long good_tsc_count = 0;
 221        unsigned long delay_calibration_ticks = ((REAL_HZ < 100) ? 1 : (REAL_HZ/100));
 222        int i;
 223
 224        if (read_current_timer(&pre_start) < 0 )
 225                return 0;
 226
 227        /*
 228         * A simple loop like
 229         *      while ( jiffies < start_jiffies+1)
 230         *              start = read_current_timer();
 231         * will not do. As we don't really know whether jiffy switch
 232         * happened first or timer_value was read first. And some asynchronous
 233         * event can happen between these two events introducing errors in lpj.
 234         *
 235         * So, we do
 236         * 1. pre_start <- When we are sure that jiffy switch hasn't happened
 237         * 2. check jiffy switch
 238         * 3. start <- timer value before or after jiffy switch
 239         * 4. post_start <- When we are sure that jiffy switch has happened
 240         *
 241         * Note, we don't know anything about order of 2 and 3.
 242         * Now, by looking at post_start and pre_start difference, we can
 243         * check whether any asynchronous event happened or not
 244         */
 245
 246        for (i = 0; i < MAX_DIRECT_CALIBRATION_RETRIES; i++) {
 247                pre_start = 0;
 248                read_current_timer(&start);
 249                start_jiffies = jiffies;
 250                while (jiffies <= (start_jiffies + tick_divider)) {
 251                        pre_start = start;
 252                        read_current_timer(&start);
 253                }
 254                read_current_timer(&post_start);
 255
 256                pre_end = 0;
 257                end = post_start;
 258                while (jiffies <=
 259                       (start_jiffies + tick_divider *
 260                       (1 + delay_calibration_ticks))) {
 261                        pre_end = end;
 262                        read_current_timer(&end);
 263                }
 264                read_current_timer(&post_end);
 265
 266                tsc_rate_max = (post_end - pre_start) / delay_calibration_ticks;
 267                tsc_rate_min = (pre_end - post_start) / delay_calibration_ticks;
 268
 269                tsc_rate_max /= tick_divider;
 270                tsc_rate_min /= tick_divider;
 271
 272                /*
 273                 * If the upper limit and lower limit of the tsc_rate is
 274                 * >= 12.5% apart, redo calibration.
 275                 */
 276                if (pre_start != 0 && pre_end != 0 &&
 277                    (tsc_rate_max - tsc_rate_min) < (tsc_rate_max >> 3)) {
 278                        good_tsc_count++;
 279                        good_tsc_sum += tsc_rate_max;
 280                }
 281        }
 282
 283        if (good_tsc_count)
 284                return (good_tsc_sum/good_tsc_count);
 285
 286        printk(KERN_WARNING "calibrate_delay_direct() failed to get a good "
 287               "estimate for loops_per_jiffy.\nProbably due to long platform interrupts. Consider using \"lpj=\" boot option.\n");
 288        return 0;
 289}
 290#else
 291static unsigned long __devinit calibrate_delay_direct(void) {return 0;}
 292#endif
 293
 294/*
 295 * This is the number of bits of precision for the loops_per_jiffy.  Each
 296 * bit takes on average 1.5/HZ seconds.  This (like the original) is a little
 297 * better than 1%
 298 */
 299#define LPS_PREC 8
 300
 301void __devinit calibrate_delay(void)
 302{
 303        unsigned long ticks, loopbit;
 304        int lps_precision = LPS_PREC;
 305
 306        if (preset_lpj) {
 307                loops_per_jiffy = preset_lpj;
 308                printk("Calibrating delay loop (skipped)... "
 309                        "%lu.%02lu BogoMIPS preset\n",
 310                        loops_per_jiffy/(500000/HZ),
 311                        (loops_per_jiffy/(5000/HZ)) % 100);
 312        } else if ((loops_per_jiffy = calibrate_delay_direct()) != 0) {
 313                printk("Calibrating delay using timer specific routine.. ");
 314                printk("%lu.%02lu BogoMIPS (lpj=%lu)\n",
 315                        loops_per_jiffy/(500000/HZ),
 316                        (loops_per_jiffy/(5000/HZ)) % 100,
 317                        loops_per_jiffy);
 318        } else {
 319                loops_per_jiffy = (1<<12);
 320
 321                printk(KERN_DEBUG "Calibrating delay loop... ");
 322                while ((loops_per_jiffy <<= 1) != 0) {
 323                        /* wait for "start of" clock tick */
 324                        ticks = jiffies;
 325                        while (ticks == jiffies)
 326                                /* nothing */;
 327                        /* Go .. */
 328                        ticks = jiffies;
 329                        __delay(loops_per_jiffy);
 330                        ticks = jiffies - ticks;
 331                        if (ticks)
 332                                break;
 333                }
 334
 335                /*
 336                 * Do a binary approximation to get loops_per_jiffy set to
 337                 * equal one clock (up to lps_precision bits)
 338                 */
 339                loops_per_jiffy >>= 1;
 340                loopbit = loops_per_jiffy;
 341                while (lps_precision-- && (loopbit >>= 1)) {
 342                        loops_per_jiffy |= loopbit;
 343                        ticks = jiffies;
 344                        while (ticks == jiffies)
 345                                /* nothing */;
 346                        ticks = jiffies;
 347                        __delay(loops_per_jiffy);
 348                        if (jiffies != ticks)   /* longer than 1 tick */
 349                                loops_per_jiffy &= ~loopbit;
 350                }
 351
 352                /* Round the value and print it */
 353                printk("%lu.%02lu BogoMIPS (lpj=%lu)\n",
 354                        loops_per_jiffy/(500000/HZ),
 355                        (loops_per_jiffy/(5000/HZ)) % 100,
 356                        loops_per_jiffy);
 357        }
 358
 359}
 360
 361static int __init debug_kernel(char *str)
 362{
 363        if (*str)
 364                return 0;
 365        console_loglevel = 10;
 366        return 1;
 367}
 368
 369static int __init quiet_kernel(char *str)
 370{
 371        if (*str)
 372                return 0;
 373        console_loglevel = 4;
 374        return 1;
 375}
 376
 377__setup("debug", debug_kernel);
 378__setup("quiet", quiet_kernel);
 379
 380/*
 381 * Unknown boot options get handed to init, unless they look like
 382 * failed parameters
 383 */
 384static int __init unknown_bootoption(char *param, char *val)
 385{
 386        /* Change NUL term back to "=", to make "param" the whole string. */
 387        if (val) {
 388                if (val[-1] == '"') val[-2] = '=';
 389                else val[-1] = '=';
 390        }
 391
 392        /* Handle obsolete-style parameters */
 393        if (obsolete_checksetup(param))
 394                return 0;
 395
 396        /*
 397         * Preemptive maintenance for "why didn't my mispelled command
 398         * line work?"
 399         */
 400        if (strchr(param, '.') && (!val || strchr(param, '.') < val)) {
 401                printk(KERN_ERR "Unknown boot option `%s': ignoring\n", param);
 402                return 0;
 403        }
 404
 405        if (panic_later)
 406                return 0;
 407
 408        if (val) {
 409                /* Environment option */
 410                unsigned int i;
 411                for (i = 0; envp_init[i]; i++) {
 412                        if (i == MAX_INIT_ENVS) {
 413                                panic_later = "Too many boot env vars at `%s'";
 414                                panic_param = param;
 415                        }
 416                        if (!strncmp(param, envp_init[i], val - param))
 417                                break;
 418                }
 419                envp_init[i] = param;
 420        } else {
 421                /* Command line option */
 422                unsigned int i;
 423                for (i = 0; argv_init[i]; i++) {
 424                        if (i == MAX_INIT_ARGS) {
 425                                panic_later = "Too many boot init vars at `%s'";
 426                                panic_param = param;
 427                        }
 428                }
 429                argv_init[i] = param;
 430        }
 431        return 0;
 432}
 433
 434static int __init init_setup(char *str)
 435{
 436        unsigned int i;
 437
 438        execute_command = str;
 439        /*
 440         * In case LILO is going to boot us with default command line,
 441         * it prepends "auto" before the whole cmdline which makes
 442         * the shell think it should execute a script with such name.
 443         * So we ignore all arguments entered _before_ init=... [MJ]
 444         */
 445        for (i = 1; i < MAX_INIT_ARGS; i++)
 446                argv_init[i] = NULL;
 447        return 1;
 448}
 449__setup("init=", init_setup);
 450
 451extern void setup_arch(char **);
 452extern void cpu_idle(void);
 453
 454#ifndef CONFIG_SMP
 455
 456#ifdef CONFIG_X86_LOCAL_APIC
 457static void __init smp_init(void)
 458{
 459        APIC_init_uniprocessor();
 460}
 461#else
 462#define smp_init()      do { } while (0)
 463#endif
 464
 465static inline void setup_per_cpu_areas(void) { }
 466static inline void smp_prepare_cpus(unsigned int maxcpus) { }
 467
 468#else
 469
 470#ifdef __GENERIC_PER_CPU
 471unsigned long __per_cpu_offset[NR_CPUS];
 472
 473EXPORT_SYMBOL(__per_cpu_offset);
 474
 475static void __init setup_per_cpu_areas(void)
 476{
 477        unsigned long size, i;
 478        char *ptr;
 479        /* Created by linker magic */
 480        extern char __per_cpu_start[], __per_cpu_end[];
 481
 482        /* Copy section for each CPU (we discard the original) */
 483        size = ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES);
 484#ifdef CONFIG_MODULES
 485        if (size < PERCPU_ENOUGH_ROOM)
 486                size = PERCPU_ENOUGH_ROOM;
 487#endif
 488
 489        ptr = alloc_bootmem(size * NR_CPUS);
 490
 491        for (i = 0; i < NR_CPUS; i++, ptr += size) {
 492                __per_cpu_offset[i] = ptr - __per_cpu_start;
 493                memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
 494        }
 495}
 496#endif /* !__GENERIC_PER_CPU */
 497
 498/* Called by boot processor to activate the rest. */
 499static void __init smp_init(void)
 500{
 501        unsigned int i;
 502
 503        /* FIXME: This should be done in userspace --RR */
 504        for_each_present_cpu(i) {
 505                if (num_online_cpus() >= max_cpus)
 506                        break;
 507                if (!cpu_online(i))
 508                        cpu_up(i);
 509        }
 510
 511        /* Any cleanup work */
 512        printk("Brought up %ld CPUs\n", (long)num_online_cpus());
 513        smp_cpus_done(max_cpus);
 514#if 0
 515        /* Get other processors into their bootup holding patterns. */
 516
 517        smp_threads_ready=1;
 518        smp_commence();
 519#endif
 520}
 521
 522#endif
 523
 524/*
 525 * We need to finalize in a non-__init function or else race conditions
 526 * between the root thread and the init thread may cause start_kernel to
 527 * be reaped by free_initmem before the root thread has proceeded to
 528 * cpu_idle.
 529 *
 530 * gcc-3.4 accidentally inlines this function, so use noinline.
 531 */
 532
 533static void noinline rest_init(void)
 534{
 535        kernel_thread(init, NULL, CLONE_FS | CLONE_SIGHAND);
 536        numa_default_policy();
 537        system_state = SYSTEM_BOOTING_SCHEDULER_OK;
 538        unlock_kernel();
 539        cpu_idle();
 540} 
 541
 542/* Check for early params. */
 543static int __init do_early_param(char *param, char *val)
 544{
 545        struct obs_kernel_param *p;
 546        extern struct obs_kernel_param __setup_start, __setup_end;
 547
 548        for (p = &__setup_start; p < &__setup_end; p++) {
 549                if (p->early && strcmp(param, p->str) == 0) {
 550                        if (p->setup_func(val) != 0)
 551                                printk(KERN_WARNING
 552                                       "Malformed early option '%s'\n", param);
 553                }
 554        }
 555        /* We accept everything at this stage. */
 556        return 0;
 557}
 558
 559/* Arch code calls this early on, or if not, just before other parsing. */
 560void __init parse_early_param(void)
 561{
 562        static __initdata int done = 0;
 563        static __initdata char tmp_cmdline[COMMAND_LINE_SIZE];
 564
 565        if (done)
 566                return;
 567
 568        /* All fall through to do_early_param. */
 569        strlcpy(tmp_cmdline, saved_command_line, COMMAND_LINE_SIZE);
 570        parse_args("early options", tmp_cmdline, NULL, 0, do_early_param);
 571        done = 1;
 572}
 573
 574/*
 575 *      Activate the first processor.
 576 */
 577
 578asmlinkage void __init start_kernel(void)
 579{
 580        char * command_line;
 581        extern struct kernel_param __start___param[], __stop___param[];
 582/*
 583 * Interrupts are still disabled. Do necessary setups, then
 584 * enable them
 585 */
 586        lock_kernel();
 587        page_address_init();
 588        printk(linux_banner);
 589        setup_arch(&command_line);
 590        setup_per_cpu_areas();
 591
 592        /*
 593         * Mark the boot cpu "online" so that it can call console drivers in
 594         * printk() and can access its per-cpu storage.
 595         */
 596        smp_prepare_boot_cpu();
 597
 598        /*
 599         * Set up the scheduler prior starting any interrupts (such as the
 600         * timer interrupt). Full topology setup happens at smp_init()
 601         * time - but meanwhile we still have a functioning scheduler.
 602         */
 603        sched_init();
 604        build_all_zonelists();
 605        page_alloc_init();
 606        printk("Kernel command line: %s\n", saved_command_line);
 607        parse_early_param();
 608        parse_args("Booting kernel", command_line, __start___param,
 609                   __stop___param - __start___param,
 610                   &unknown_bootoption);
 611        sort_main_extable();
 612        trap_init();
 613        rcu_init();
 614        init_IRQ();
 615        pidhash_init();
 616        init_timers();
 617        softirq_init();
 618        time_init();
 619
 620        /*
 621         * HACK ALERT! This is early. We're enabling the console before
 622         * we've done PCI setups etc, and console_init() must be aware of
 623         * this. But we do want output early, in case something goes wrong.
 624         */
 625        console_init();
 626        if (panic_later)
 627                panic(panic_later, panic_param);
 628        profile_init();
 629        local_irq_enable();
 630#ifdef CONFIG_BLK_DEV_INITRD
 631        if (initrd_start && !initrd_below_start_ok &&
 632                        initrd_start < min_low_pfn << PAGE_SHIFT) {
 633                printk(KERN_CRIT "initrd overwritten (0x%08lx < 0x%08lx) - "
 634                    "disabling it.\n",initrd_start,min_low_pfn << PAGE_SHIFT);
 635                initrd_start = 0;
 636        }
 637#endif
 638        vfs_caches_init_early();
 639        mem_init();
 640        kmem_cache_init();
 641        numa_policy_init();
 642        if (late_time_init)
 643                late_time_init();
 644        calibrate_delay();
 645        pidmap_init();
 646        pgtable_cache_init();
 647        prio_tree_init();
 648        anon_vma_init();
 649#ifdef CONFIG_X86
 650        if (efi_enabled)
 651                efi_enter_virtual_mode();
 652#endif
 653        fork_init(num_physpages);
 654        proc_caches_init();
 655        buffer_init();
 656        unnamed_dev_init();
 657        key_init();
 658        security_scaffolding_startup();
 659        vfs_caches_init(num_physpages);
 660        radix_tree_init();
 661        signals_init();
 662        /* rootfs populating might need page-writeback */
 663        page_writeback_init();
 664#ifdef CONFIG_PROC_FS
 665        proc_root_init();
 666#endif
 667        check_bugs();
 668
 669        acpi_early_init(); /* before LAPIC and SMP init */
 670
 671        /* Do the rest non-__init'ed, we're now alive */
 672        rest_init();
 673}
 674
 675static int __initdata initcall_debug;
 676
 677static int __init initcall_debug_setup(char *str)
 678{
 679        initcall_debug = 1;
 680        return 1;
 681}
 682__setup("initcall_debug", initcall_debug_setup);
 683
 684struct task_struct *child_reaper = &init_task;
 685
 686extern initcall_t __initcall_start, __initcall_end;
 687
 688static void __init do_initcalls(void)
 689{
 690        initcall_t *call;
 691        int count = preempt_count();
 692
 693        for (call = &__initcall_start; call < &__initcall_end; call++) {
 694                char *msg;
 695
 696                if (initcall_debug) {
 697                        printk(KERN_DEBUG "Calling initcall 0x%p", *call);
 698                        print_fn_descriptor_symbol(": %s()",
 699                                                   (unsigned long) *call);
 700                        printk("\n");
 701                }
 702
 703                (*call)();
 704
 705                msg = NULL;
 706                if (preempt_count() != count) {
 707                        msg = "preemption imbalance";
 708                        preempt_count() = count;
 709                }
 710                if (irqs_disabled()) {
 711                        msg = "disabled interrupts";
 712                        local_irq_enable();
 713                }
 714                if (msg) {
 715                        printk("error in initcall at 0x%p: "
 716                                "returned with %s\n", *call, msg);
 717                }
 718        }
 719
 720        /* Make sure there is no pending stuff from the initcall sequence */
 721        flush_scheduled_work();
 722}
 723
 724/*
 725 * Ok, the machine is now initialized. None of the devices
 726 * have been touched yet, but the CPU subsystem is up and
 727 * running, and memory and process management works.
 728 *
 729 * Now we can finally start doing some real work..
 730 */
 731static void __init do_basic_setup(void)
 732{
 733        /* drivers will send hotplug events */
 734        init_workqueues();
 735        usermodehelper_init();
 736
 737        driver_init();
 738
 739#ifdef CONFIG_SYSCTL
 740        sysctl_init();
 741#endif
 742
 743        /* Networking initialization needs a process context */ 
 744        sock_init();
 745
 746        do_initcalls();
 747}
 748
 749static void do_pre_smp_initcalls(void)
 750{
 751        extern int spawn_ksoftirqd(void);
 752#ifdef CONFIG_SMP
 753        extern int migration_init(void);
 754
 755        migration_init();
 756#endif
 757        spawn_ksoftirqd();
 758}
 759
 760static void run_init_process(char *init_filename)
 761{
 762        argv_init[0] = init_filename;
 763        execve(init_filename, argv_init, envp_init);
 764}
 765
 766static inline void fixup_cpu_present_map(void)
 767{
 768#ifdef CONFIG_SMP
 769        int i;
 770
 771        /*
 772         * If arch is not hotplug ready and did not populate
 773         * cpu_present_map, just make cpu_present_map same as cpu_possible_map
 774         * for other cpu bringup code to function as normal. e.g smp_init() etc.
 775         */
 776        if (cpus_empty(cpu_present_map)) {
 777                for_each_cpu(i) {
 778                        cpu_set(i, cpu_present_map);
 779                }
 780        }
 781#endif
 782}
 783
 784static int init(void * unused)
 785{
 786        lock_kernel();
 787        /*
 788         * Tell the world that we're going to be the grim
 789         * reaper of innocent orphaned children.
 790         *
 791         * We don't want people to have to make incorrect
 792         * assumptions about where in the task array this
 793         * can be found.
 794         */
 795        child_reaper = current;
 796
 797        /* Sets up cpus_possible() */
 798        smp_prepare_cpus(max_cpus);
 799
 800        do_pre_smp_initcalls();
 801
 802        fixup_cpu_present_map();
 803        smp_init();
 804
 805        /*
 806         * Do this before initcalls, because some drivers want to access
 807         * firmware files.
 808         */
 809        populate_rootfs();
 810
 811        do_basic_setup();
 812
 813        sched_init_smp();
 814
 815        /*
 816         * check if there is an early userspace init.  If yes, let it do all
 817         * the work
 818         */
 819        if (sys_access((const char __user *) "/init", 0) == 0)
 820                execute_command = "/init";
 821        else
 822                prepare_namespace();
 823
 824        /*
 825         * Ok, we have completed the initial bootup, and
 826         * we're essentially up and running. Get rid of the
 827         * initmem segments and start the user-mode stuff..
 828         */
 829        free_initmem();
 830        unlock_kernel();
 831        system_state = SYSTEM_RUNNING;
 832        numa_default_policy();
 833
 834        if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
 835                printk("Warning: unable to open an initial console.\n");
 836
 837        (void) sys_dup(0);
 838        (void) sys_dup(0);
 839        
 840        /*
 841         * We try each of these until one succeeds.
 842         *
 843         * The Bourne shell can be used instead of init if we are 
 844         * trying to recover a really broken machine.
 845         */
 846
 847        if (execute_command)
 848                run_init_process(execute_command);
 849
 850        run_init_process("/sbin/init");
 851        run_init_process("/etc/init");
 852        run_init_process("/bin/init");
 853        run_init_process("/bin/sh");
 854
 855        panic("No init found.  Try passing init= option to kernel.");
 856}
 857
 858static int early_param_test(char *rest)
 859{
 860        printk("early_parm_test: %s\n", rest ?: "(null)");
 861        return rest ? 0 : -EINVAL;
 862}
 863early_param("testsetup", early_param_test);
 864static int early_setup_test(char *rest)
 865{
 866        printk("early_setup_test: %s\n", rest ?: "(null)");
 867        return 0;
 868}
 869__setup("testsetup_long", early_setup_test);
 870