main.c 4.1 KB

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