main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. *
  6. * This file is part of the Linux kernel, and is made available under
  7. * the terms of the GNU General Public License version 2.
  8. *
  9. * ----------------------------------------------------------------------- */
  10. /*
  11. * arch/i386/boot/main.c
  12. *
  13. * Main module for the real-mode kernel code
  14. */
  15. #include "boot.h"
  16. struct boot_params boot_params __attribute__((aligned(16)));
  17. char *HEAP = _end;
  18. char *heap_end = _end; /* Default end of heap = no heap */
  19. /*
  20. * Copy the header into the boot parameter block. Since this
  21. * screws up the old-style command line protocol, adjust by
  22. * filling in the new-style command line pointer instead.
  23. */
  24. static void copy_boot_params(void)
  25. {
  26. struct old_cmdline {
  27. u16 cl_magic;
  28. u16 cl_offset;
  29. };
  30. const struct old_cmdline * const oldcmd =
  31. (const struct old_cmdline *)OLD_CL_ADDRESS;
  32. BUILD_BUG_ON(sizeof boot_params != 4096);
  33. memcpy(&boot_params.hdr, &hdr, sizeof hdr);
  34. if (!boot_params.hdr.cmd_line_ptr &&
  35. oldcmd->cl_magic == OLD_CL_MAGIC) {
  36. /* Old-style command line protocol. */
  37. u16 cmdline_seg;
  38. /* Figure out if the command line falls in the region
  39. of memory that an old kernel would have copied up
  40. to 0x90000... */
  41. if (oldcmd->cl_offset < boot_params.hdr.setup_move_size)
  42. cmdline_seg = ds();
  43. else
  44. cmdline_seg = 0x9000;
  45. boot_params.hdr.cmd_line_ptr =
  46. (cmdline_seg << 4) + oldcmd->cl_offset;
  47. }
  48. }
  49. /*
  50. * Set the keyboard repeat rate to maximum. Unclear why this
  51. * is done here; this might be possible to kill off as stale code.
  52. */
  53. static void keyboard_set_repeat(void)
  54. {
  55. u16 ax = 0x0305;
  56. u16 bx = 0;
  57. asm volatile("int $0x16"
  58. : "+a" (ax), "+b" (bx)
  59. : : "ecx", "edx", "esi", "edi");
  60. }
  61. /*
  62. * Get Intel SpeedStep (IST) information.
  63. */
  64. static void query_ist(void)
  65. {
  66. asm("int $0x15"
  67. : "=a" (boot_params.ist_info.signature),
  68. "=b" (boot_params.ist_info.command),
  69. "=c" (boot_params.ist_info.event),
  70. "=d" (boot_params.ist_info.perf_level)
  71. : "a" (0x0000e980), /* IST Support */
  72. "d" (0x47534943)); /* Request value */
  73. }
  74. /*
  75. * Tell the BIOS what CPU mode we intend to run in.
  76. */
  77. static void set_bios_mode(void)
  78. {
  79. #ifdef CONFIG_X86_64
  80. u32 eax, ebx;
  81. eax = 0xec00;
  82. ebx = 2;
  83. asm volatile("int $0x15"
  84. : "+a" (eax), "+b" (ebx)
  85. : : "ecx", "edx", "esi", "edi");
  86. #endif
  87. }
  88. static void init_heap(void)
  89. {
  90. char *stack_end;
  91. if (boot_params.hdr.loadflags & CAN_USE_HEAP) {
  92. asm("leal %P1(%%esp),%0"
  93. : "=r" (stack_end) : "i" (-STACK_SIZE));
  94. heap_end = (char *)
  95. ((size_t)boot_params.hdr.heap_end_ptr + 0x200);
  96. if (heap_end > stack_end)
  97. heap_end = stack_end;
  98. } else {
  99. /* Boot protocol 2.00 only, no heap available */
  100. puts("WARNING: Ancient bootloader, some functionality "
  101. "may be limited!\n");
  102. }
  103. }
  104. void main(void)
  105. {
  106. /* First, copy the boot header into the "zeropage" */
  107. copy_boot_params();
  108. /* End of heap check */
  109. init_heap();
  110. /* Make sure we have all the proper CPU support */
  111. if (validate_cpu()) {
  112. puts("Unable to boot - please use a kernel appropriate "
  113. "for your CPU.\n");
  114. die();
  115. }
  116. /* Tell the BIOS what CPU mode we intend to run in. */
  117. set_bios_mode();
  118. /* Detect memory layout */
  119. detect_memory();
  120. /* Set keyboard repeat rate (why?) */
  121. keyboard_set_repeat();
  122. /* Query MCA information */
  123. query_mca();
  124. /* Voyager */
  125. #ifdef CONFIG_X86_VOYAGER
  126. query_voyager();
  127. #endif
  128. /* Query Intel SpeedStep (IST) information */
  129. query_ist();
  130. /* Query APM information */
  131. #if defined(CONFIG_APM) || defined(CONFIG_APM_MODULE)
  132. query_apm_bios();
  133. #endif
  134. /* Query EDD information */
  135. #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
  136. query_edd();
  137. #endif
  138. /* Set the video mode */
  139. set_video();
  140. /* Do the last things and invoke protected mode */
  141. go_to_protected_mode();
  142. }