From fa97bdf92709adaaf8b9a5164a895e262a4fcf60 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Sun, 11 Jul 2010 11:06:57 +0300 Subject: [PATCH 1/7] x86, setup: Early-boot serial I/O support This patch adds serial I/O support to the real-mode setup (very early boot) printf(). It's useful for debugging boot code when running Linux under KVM, for example. The actual code was lifted from early printk. Cc: Cyrill Gorcunov Cc: Ingo Molnar Cc: Yinghai Lu Signed-off-by: Pekka Enberg LKML-Reference: <1278835617-11368-1-git-send-email-penberg@cs.helsinki.fi> Signed-off-by: H. Peter Anvin --- arch/x86/boot/boot.h | 16 ++++++ arch/x86/boot/main.c | 3 ++ arch/x86/boot/string.c | 41 +++++++++++++++ arch/x86/boot/tty.c | 111 ++++++++++++++++++++++++++++++++++++++--- 4 files changed, 165 insertions(+), 6 deletions(-) diff --git a/arch/x86/boot/boot.h b/arch/x86/boot/boot.h index 98239d2658f2..46c4c5c71af7 100644 --- a/arch/x86/boot/boot.h +++ b/arch/x86/boot/boot.h @@ -37,6 +37,8 @@ extern struct setup_header hdr; extern struct boot_params boot_params; +#define cpu_relax() asm volatile("rep; nop") + /* Basic port I/O */ static inline void outb(u8 v, u16 port) { @@ -203,6 +205,17 @@ static inline int isdigit(int ch) return (ch >= '0') && (ch <= '9'); } +static inline int isxdigit(int ch) +{ + if (isdigit(ch)) + return true; + + if ((ch >= 'a') && (ch <= 'f')) + return true; + + return (ch >= 'A') && (ch <= 'F'); +} + /* Heap -- available for dynamic lists. */ extern char _end[]; extern char *HEAP; @@ -329,10 +342,13 @@ void initregs(struct biosregs *regs); /* string.c */ int strcmp(const char *str1, const char *str2); +int strncmp(const char *cs, const char *ct, size_t count); size_t strnlen(const char *s, size_t maxlen); unsigned int atou(const char *s); +unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base); /* tty.c */ +void console_init(void); void puts(const char *); void putchar(int); int getchar(void); diff --git a/arch/x86/boot/main.c b/arch/x86/boot/main.c index 140172b895bd..4ef1a33e8572 100644 --- a/arch/x86/boot/main.c +++ b/arch/x86/boot/main.c @@ -130,6 +130,9 @@ void main(void) /* First, copy the boot header into the "zeropage" */ copy_boot_params(); + /* Initialize the early-boot console */ + console_init(); + /* End of heap check */ init_heap(); diff --git a/arch/x86/boot/string.c b/arch/x86/boot/string.c index f94b7a0c2abf..aba29df4a7bb 100644 --- a/arch/x86/boot/string.c +++ b/arch/x86/boot/string.c @@ -30,6 +30,22 @@ int strcmp(const char *str1, const char *str2) return 0; } +int strncmp(const char *cs, const char *ct, size_t count) +{ + unsigned char c1, c2; + + while (count) { + c1 = *cs++; + c2 = *ct++; + if (c1 != c2) + return c1 < c2 ? -1 : 1; + if (!c1) + break; + count--; + } + return 0; +} + size_t strnlen(const char *s, size_t maxlen) { const char *es = s; @@ -48,3 +64,28 @@ unsigned int atou(const char *s) i = i * 10 + (*s++ - '0'); return i; } + +/* Works only for digits and letters, but small and fast */ +#define TOLOWER(x) ((x) | 0x20) + +unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) +{ + unsigned long long result = 0; + + if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') + cp += 2; + + while (isxdigit(*cp)) { + unsigned int value; + + value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; + if (value >= base) + break; + result = result * base + value; + cp++; + } + if (endp) + *endp = (char *)cp; + + return result; +} diff --git a/arch/x86/boot/tty.c b/arch/x86/boot/tty.c index 01ec69c901c7..f3ceee20ff12 100644 --- a/arch/x86/boot/tty.c +++ b/arch/x86/boot/tty.c @@ -10,24 +10,52 @@ * ----------------------------------------------------------------------- */ /* - * Very simple screen I/O - * XXX: Probably should add very simple serial I/O? + * Very simple screen and serial I/O */ #include "boot.h" +#define DEFAULT_SERIAL_PORT 0x3f8 /* ttyS0 */ + +static int early_serial_base; + +#define XMTRDY 0x20 + +#define DLAB 0x80 + +#define TXR 0 /* Transmit register (WRITE) */ +#define RXR 0 /* Receive register (READ) */ +#define IER 1 /* Interrupt Enable */ +#define IIR 2 /* Interrupt ID */ +#define FCR 2 /* FIFO control */ +#define LCR 3 /* Line control */ +#define MCR 4 /* Modem control */ +#define LSR 5 /* Line Status */ +#define MSR 6 /* Modem Status */ +#define DLL 0 /* Divisor Latch Low */ +#define DLH 1 /* Divisor latch High */ + +#define DEFAULT_BAUD 9600 + /* * These functions are in .inittext so they can be used to signal * error during initialization. */ -void __attribute__((section(".inittext"))) putchar(int ch) +static void __attribute__((section(".inittext"))) serial_putchar(int ch) +{ + unsigned timeout = 0xffff; + + while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout) + cpu_relax(); + + outb(ch, early_serial_base + TXR); +} + +static void __attribute__((section(".inittext"))) bios_putchar(int ch) { struct biosregs ireg; - if (ch == '\n') - putchar('\r'); /* \n -> \r\n */ - initregs(&ireg); ireg.bx = 0x0007; ireg.cx = 0x0001; @@ -36,6 +64,17 @@ void __attribute__((section(".inittext"))) putchar(int ch) intcall(0x10, &ireg, NULL); } +void __attribute__((section(".inittext"))) putchar(int ch) +{ + if (ch == '\n') + putchar('\r'); /* \n -> \r\n */ + + bios_putchar(ch); + + if (early_serial_base != 0) + serial_putchar(ch); +} + void __attribute__((section(".inittext"))) puts(const char *str) { while (*str) @@ -112,3 +151,63 @@ int getchar_timeout(void) return 0; /* Timeout! */ } + +static void early_serial_init(int baud) +{ + unsigned char c; + unsigned divisor; + + outb(0x3, early_serial_base + LCR); /* 8n1 */ + outb(0, early_serial_base + IER); /* no interrupt */ + outb(0, early_serial_base + FCR); /* no fifo */ + outb(0x3, early_serial_base + MCR); /* DTR + RTS */ + + divisor = 115200 / baud; + c = inb(early_serial_base + LCR); + outb(c | DLAB, early_serial_base + LCR); + outb(divisor & 0xff, early_serial_base + DLL); + outb((divisor >> 8) & 0xff, early_serial_base + DLH); + outb(c & ~DLAB, early_serial_base + LCR); +} + +void console_init(void) +{ + int baud = DEFAULT_BAUD; + char arg[32]; + int pos = 0; + + if (cmdline_find_option("earlyprintk", arg, sizeof arg) > 0) { + char *e; + + if (!strncmp(arg, "serial", 6)) { + early_serial_base = DEFAULT_SERIAL_PORT; + pos += 6; + } + + if (arg[pos] == ',') + pos++; + + if (!strncmp(arg, "ttyS", 4)) { + static const int bases[] = { 0x3f8, 0x2f8 }; + int port = 0; + + if (!strncmp(arg + pos, "ttyS", 4)) + pos += 4; + + if (arg[pos++] == '1') + port = 1; + + early_serial_base = bases[port]; + } + + if (arg[pos] == ',') + pos++; + + baud = simple_strtoull(arg + pos, &e, 0); + if (baud == 0 || arg + pos == e) + baud = DEFAULT_BAUD; + } + + if (early_serial_base != 0) + early_serial_init(baud); +} From ce0aa5dd20e44372f9617dd67c984f41fcdbed88 Mon Sep 17 00:00:00 2001 From: Yinghai Lu Date: Tue, 13 Jul 2010 13:35:17 -0700 Subject: [PATCH 2/7] x86, setup: Make the setup code also accept console=uart8250 Make the boot code also accept the console=uart8250,io,0x2f8,115200n form of early console. Also add back simple_guess_base(), otherwise those simple_strtoull(,,0) are not going to work. Signed-off-by: Yinghai Lu LKML-Reference: <4C3CCE05.4090505@kernel.org> Acked-by: Pekka Enberg Signed-off-by: H. Peter Anvin --- arch/x86/boot/string.c | 22 ++++++++++++++++ arch/x86/boot/tty.c | 59 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/arch/x86/boot/string.c b/arch/x86/boot/string.c index aba29df4a7bb..3cbc4058dd26 100644 --- a/arch/x86/boot/string.c +++ b/arch/x86/boot/string.c @@ -68,10 +68,32 @@ unsigned int atou(const char *s) /* Works only for digits and letters, but small and fast */ #define TOLOWER(x) ((x) | 0x20) +static unsigned int simple_guess_base(const char *cp) +{ + if (cp[0] == '0') { + if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2])) + return 16; + else + return 8; + } else { + return 10; + } +} + +/** + * simple_strtoull - convert a string to an unsigned long long + * @cp: The start of the string + * @endp: A pointer to the end of the parsed string will be placed here + * @base: The number base to use + */ + unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) { unsigned long long result = 0; + if (!base) + base = simple_guess_base(cp); + if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') cp += 2; diff --git a/arch/x86/boot/tty.c b/arch/x86/boot/tty.c index f3ceee20ff12..f6d52e65f97a 100644 --- a/arch/x86/boot/tty.c +++ b/arch/x86/boot/tty.c @@ -170,7 +170,7 @@ static void early_serial_init(int baud) outb(c & ~DLAB, early_serial_base + LCR); } -void console_init(void) +static int parse_earlyprintk(void) { int baud = DEFAULT_BAUD; char arg[32]; @@ -208,6 +208,63 @@ void console_init(void) baud = DEFAULT_BAUD; } + return baud; +} + +#define BASE_BAUD (1843200/16) +static unsigned int probe_baud(int port) +{ + unsigned char lcr, dll, dlh; + unsigned int quot; + + lcr = inb(port + LCR); + outb(lcr | DLAB, port + LCR); + dll = inb(port + DLL); + dlh = inb(port + DLH); + outb(lcr, port + LCR); + quot = (dlh << 8) | dll; + + return BASE_BAUD / quot; +} + +static int parse_console_uart8250(void) +{ + char optstr[64], *options; + int baud = DEFAULT_BAUD; + + /* + * console=uart8250,io,0x3f8,115200n8 + * need to make sure it is last one console ! + */ + if (cmdline_find_option("console", optstr, sizeof optstr) <= 0) + return baud; + + options = optstr; + + if (!strncmp(options, "uart8250,io,", 12)) + early_serial_base = simple_strtoull(options + 12, &options, 0); + else if (!strncmp(options, "uart,io,", 8)) + early_serial_base = simple_strtoull(options + 8, &options, 0); + else + return baud; + + if (options && (options[0] == ',')) + baud = simple_strtoull(options + 1, &options, 0); + else + baud = probe_baud(early_serial_base); + + return baud; +} + +void console_init(void) +{ + int baud; + + baud = parse_earlyprintk(); + + if (!early_serial_base) + baud = parse_console_uart8250(); + if (early_serial_base != 0) early_serial_init(baud); } From 70b0d22d581a5deef7b2876b0c3774635b8d846c Mon Sep 17 00:00:00 2001 From: Yinghai Lu Date: Wed, 14 Jul 2010 11:26:57 -0700 Subject: [PATCH 3/7] x86, setup: Only set early_serial_base after port is initialized putchar is using early_serial_base to check if port is initialized. So we only assign it after early_serial_init() is called, in case we need use VGA to debug early serial console. Also add display for port addr and baud. -v2: update to current tip Acked-by: Pekka Enberg Signed-off-by: Yinghai Lu LKML-Reference: <4C3E0171.6050008@kernel.org> Signed-off-by: H. Peter Anvin --- arch/x86/boot/tty.c | 63 ++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/arch/x86/boot/tty.c b/arch/x86/boot/tty.c index f6d52e65f97a..ff4b27a0fc5e 100644 --- a/arch/x86/boot/tty.c +++ b/arch/x86/boot/tty.c @@ -152,35 +152,40 @@ int getchar_timeout(void) return 0; /* Timeout! */ } -static void early_serial_init(int baud) +static void early_serial_init(int port, int baud) { unsigned char c; unsigned divisor; - outb(0x3, early_serial_base + LCR); /* 8n1 */ - outb(0, early_serial_base + IER); /* no interrupt */ - outb(0, early_serial_base + FCR); /* no fifo */ - outb(0x3, early_serial_base + MCR); /* DTR + RTS */ + outb(0x3, port + LCR); /* 8n1 */ + outb(0, port + IER); /* no interrupt */ + outb(0, port + FCR); /* no fifo */ + outb(0x3, port + MCR); /* DTR + RTS */ divisor = 115200 / baud; - c = inb(early_serial_base + LCR); - outb(c | DLAB, early_serial_base + LCR); - outb(divisor & 0xff, early_serial_base + DLL); - outb((divisor >> 8) & 0xff, early_serial_base + DLH); - outb(c & ~DLAB, early_serial_base + LCR); + c = inb(port + LCR); + outb(c | DLAB, port + LCR); + outb(divisor & 0xff, port + DLL); + outb((divisor >> 8) & 0xff, port + DLH); + outb(c & ~DLAB, port + LCR); + + early_serial_base = port; + + printf("Early serial console at I/O port 0x%x baud: %d\n", port, baud); } -static int parse_earlyprintk(void) +static void parse_earlyprintk(void) { int baud = DEFAULT_BAUD; char arg[32]; int pos = 0; + int port = 0; if (cmdline_find_option("earlyprintk", arg, sizeof arg) > 0) { char *e; if (!strncmp(arg, "serial", 6)) { - early_serial_base = DEFAULT_SERIAL_PORT; + port = DEFAULT_SERIAL_PORT; pos += 6; } @@ -189,15 +194,15 @@ static int parse_earlyprintk(void) if (!strncmp(arg, "ttyS", 4)) { static const int bases[] = { 0x3f8, 0x2f8 }; - int port = 0; + int idx = 0; if (!strncmp(arg + pos, "ttyS", 4)) pos += 4; if (arg[pos++] == '1') - port = 1; + idx = 1; - early_serial_base = bases[port]; + port = bases[idx]; } if (arg[pos] == ',') @@ -208,7 +213,8 @@ static int parse_earlyprintk(void) baud = DEFAULT_BAUD; } - return baud; + if (port) + early_serial_init(port, baud); } #define BASE_BAUD (1843200/16) @@ -227,44 +233,41 @@ static unsigned int probe_baud(int port) return BASE_BAUD / quot; } -static int parse_console_uart8250(void) +static void parse_console_uart8250(void) { char optstr[64], *options; int baud = DEFAULT_BAUD; + int port = 0; /* * console=uart8250,io,0x3f8,115200n8 * need to make sure it is last one console ! */ if (cmdline_find_option("console", optstr, sizeof optstr) <= 0) - return baud; + return; options = optstr; if (!strncmp(options, "uart8250,io,", 12)) - early_serial_base = simple_strtoull(options + 12, &options, 0); + port = simple_strtoull(options + 12, &options, 0); else if (!strncmp(options, "uart,io,", 8)) - early_serial_base = simple_strtoull(options + 8, &options, 0); + port = simple_strtoull(options + 8, &options, 0); else - return baud; + return; if (options && (options[0] == ',')) baud = simple_strtoull(options + 1, &options, 0); else - baud = probe_baud(early_serial_base); + baud = probe_baud(port); - return baud; + if (port) + early_serial_init(port, baud); } void console_init(void) { - int baud; - - baud = parse_earlyprintk(); + parse_earlyprintk(); if (!early_serial_base) - baud = parse_console_uart8250(); - - if (early_serial_base != 0) - early_serial_init(baud); + parse_console_uart8250(); } From 22a57f5896df218356bae6203dfaf04bcfd6c88c Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Mon, 2 Aug 2010 15:34:44 -0700 Subject: [PATCH 4/7] x86, setup: Allow global variables and functions in the decompressor In order for global variables and functions to work in the decompressor, we need to fix up the GOT in assembly code. Signed-off-by: H. Peter Anvin LKML-Reference: <4C57382E.8050501@zytor.com> --- arch/x86/boot/compressed/head_32.S | 13 +++++++++++++ arch/x86/boot/compressed/head_64.S | 13 +++++++++++++ arch/x86/boot/compressed/vmlinux.lds.S | 6 ++++++ 3 files changed, 32 insertions(+) diff --git a/arch/x86/boot/compressed/head_32.S b/arch/x86/boot/compressed/head_32.S index f543b70ffae2..67a655a39ce4 100644 --- a/arch/x86/boot/compressed/head_32.S +++ b/arch/x86/boot/compressed/head_32.S @@ -123,6 +123,19 @@ relocated: shrl $2, %ecx rep stosl +/* + * Adjust our own GOT + */ + leal _got(%ebx), %edx + leal _egot(%ebx), %ecx +1: + cmpl %ecx, %edx + jae 2f + addl %ebx, (%edx) + addl $4, %edx + jmp 1b +2: + /* * Do the decompression, and jump to the new kernel.. */ diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index faff0dc9c06a..52f85a196fa0 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -279,6 +279,19 @@ relocated: shrq $3, %rcx rep stosq +/* + * Adjust our own GOT + */ + leaq _got(%rip), %rdx + leaq _egot(%rip), %rcx +1: + cmpq %rcx, %rdx + jae 2f + addq %rbx, (%rdx) + addq $8, %rdx + jmp 1b +2: + /* * Do the decompression, and jump to the new kernel.. */ diff --git a/arch/x86/boot/compressed/vmlinux.lds.S b/arch/x86/boot/compressed/vmlinux.lds.S index 5ddabceee124..34d047c98284 100644 --- a/arch/x86/boot/compressed/vmlinux.lds.S +++ b/arch/x86/boot/compressed/vmlinux.lds.S @@ -41,6 +41,12 @@ SECTIONS *(.rodata.*) _erodata = . ; } + .got : { + _got = .; + KEEP(*(.got.plt)) + KEEP(*(.got)) + _egot = .; + } .data : { _data = . ; *(.data) From f4ed2877b16e8146427306aea8819adac5c88374 Mon Sep 17 00:00:00 2001 From: Yinghai Lu Date: Mon, 2 Aug 2010 02:17:31 -0700 Subject: [PATCH 5/7] x86, setup: reorganize the early console setup Separate early_serial_console from tty.c This allows for reuse of early_serial_console.c/string.c/printf.c/cmdline.c in boot/compressed/. -v2: according to hpa, don't include string.c etc -v3: compressed/misc.c must have early_serial_base as static, so move it back to tty.c for setup code Signed-off-by: Yinghai Lu LKML-Reference: <4C568D2B.205@kernel.org> Signed-off-by: H. Peter Anvin --- arch/x86/boot/Makefile | 8 +- arch/x86/boot/boot.h | 35 ++++--- arch/x86/boot/cmdline.c | 6 +- arch/x86/boot/early_serial_console.c | 139 +++++++++++++++++++++++++++ arch/x86/boot/isdigit.h | 21 ++++ arch/x86/boot/printf.c | 4 +- arch/x86/boot/tty.c | 136 +------------------------- 7 files changed, 186 insertions(+), 163 deletions(-) create mode 100644 arch/x86/boot/early_serial_console.c create mode 100644 arch/x86/boot/isdigit.h diff --git a/arch/x86/boot/Makefile b/arch/x86/boot/Makefile index ec749c2bfdd7..f7cb086b4add 100644 --- a/arch/x86/boot/Makefile +++ b/arch/x86/boot/Makefile @@ -26,10 +26,10 @@ targets := vmlinux.bin setup.bin setup.elf bzImage targets += fdimage fdimage144 fdimage288 image.iso mtools.conf subdir- := compressed -setup-y += a20.o bioscall.o cmdline.o copy.o cpu.o cpucheck.o edd.o -setup-y += header.o main.o mca.o memory.o pm.o pmjump.o -setup-y += printf.o regs.o string.o tty.o video.o video-mode.o -setup-y += version.o +setup-y += a20.o bioscall.o cmdline.o copy.o cpu.o cpucheck.o +setup-y += early_serial_console.o edd.o header.o main.o mca.o memory.o +setup-y += pm.o pmjump.o printf.o regs.o string.o tty.o video.o +setup-y += video-mode.o version.o setup-$(CONFIG_X86_APM_BOOT) += apm.o # The link order of the video-*.o modules can matter. In particular, diff --git a/arch/x86/boot/boot.h b/arch/x86/boot/boot.h index 46c4c5c71af7..00cf51cfc2e6 100644 --- a/arch/x86/boot/boot.h +++ b/arch/x86/boot/boot.h @@ -200,21 +200,7 @@ static inline int memcmp_gs(const void *s1, addr_t s2, size_t len) return diff; } -static inline int isdigit(int ch) -{ - return (ch >= '0') && (ch <= '9'); -} - -static inline int isxdigit(int ch) -{ - if (isdigit(ch)) - return true; - - if ((ch >= 'a') && (ch <= 'f')) - return true; - - return (ch >= 'A') && (ch <= 'F'); -} +#include "isdigit.h" /* Heap -- available for dynamic lists. */ extern char _end[]; @@ -300,8 +286,18 @@ struct biosregs { void intcall(u8 int_no, const struct biosregs *ireg, struct biosregs *oreg); /* cmdline.c */ -int cmdline_find_option(const char *option, char *buffer, int bufsize); -int cmdline_find_option_bool(const char *option); +int __cmdline_find_option(u32 cmdline_ptr, const char *option, char *buffer, int bufsize); +int __cmdline_find_option_bool(u32 cmdline_ptr, const char *option); +static inline int cmdline_find_option(const char *option, char *buffer, int bufsize) +{ + return __cmdline_find_option(boot_params.hdr.cmd_line_ptr, option, buffer, bufsize); +} + +static inline int cmdline_find_option_bool(const char *option) +{ + return __cmdline_find_option_bool(boot_params.hdr.cmd_line_ptr, option); +} + /* cpu.c, cpucheck.c */ struct cpu_features { @@ -313,6 +309,10 @@ extern struct cpu_features cpu; int check_cpu(int *cpu_level_ptr, int *req_level_ptr, u32 **err_flags_ptr); int validate_cpu(void); +/* early_serial_console.c */ +extern int early_serial_base; +void console_init(void); + /* edd.c */ void query_edd(void); @@ -348,7 +348,6 @@ unsigned int atou(const char *s); unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base); /* tty.c */ -void console_init(void); void puts(const char *); void putchar(int); int getchar(void); diff --git a/arch/x86/boot/cmdline.c b/arch/x86/boot/cmdline.c index a1d35634bce0..6b3b6f708c04 100644 --- a/arch/x86/boot/cmdline.c +++ b/arch/x86/boot/cmdline.c @@ -27,9 +27,8 @@ static inline int myisspace(u8 c) * Returns the length of the argument (regardless of if it was * truncated to fit in the buffer), or -1 on not found. */ -int cmdline_find_option(const char *option, char *buffer, int bufsize) +int __cmdline_find_option(u32 cmdline_ptr, const char *option, char *buffer, int bufsize) { - u32 cmdline_ptr = boot_params.hdr.cmd_line_ptr; addr_t cptr; char c; int len = -1; @@ -100,9 +99,8 @@ int cmdline_find_option(const char *option, char *buffer, int bufsize) * Returns the position of that option (starts counting with 1) * or 0 on not found */ -int cmdline_find_option_bool(const char *option) +int __cmdline_find_option_bool(u32 cmdline_ptr, const char *option) { - u32 cmdline_ptr = boot_params.hdr.cmd_line_ptr; addr_t cptr; char c; int pos = 0, wstart = 0; diff --git a/arch/x86/boot/early_serial_console.c b/arch/x86/boot/early_serial_console.c new file mode 100644 index 000000000000..030f4b93e255 --- /dev/null +++ b/arch/x86/boot/early_serial_console.c @@ -0,0 +1,139 @@ +#include "boot.h" + +#define DEFAULT_SERIAL_PORT 0x3f8 /* ttyS0 */ + +#define XMTRDY 0x20 + +#define DLAB 0x80 + +#define TXR 0 /* Transmit register (WRITE) */ +#define RXR 0 /* Receive register (READ) */ +#define IER 1 /* Interrupt Enable */ +#define IIR 2 /* Interrupt ID */ +#define FCR 2 /* FIFO control */ +#define LCR 3 /* Line control */ +#define MCR 4 /* Modem control */ +#define LSR 5 /* Line Status */ +#define MSR 6 /* Modem Status */ +#define DLL 0 /* Divisor Latch Low */ +#define DLH 1 /* Divisor latch High */ + +#define DEFAULT_BAUD 9600 + +static void early_serial_init(int port, int baud) +{ + unsigned char c; + unsigned divisor; + + outb(0x3, port + LCR); /* 8n1 */ + outb(0, port + IER); /* no interrupt */ + outb(0, port + FCR); /* no fifo */ + outb(0x3, port + MCR); /* DTR + RTS */ + + divisor = 115200 / baud; + c = inb(port + LCR); + outb(c | DLAB, port + LCR); + outb(divisor & 0xff, port + DLL); + outb((divisor >> 8) & 0xff, port + DLH); + outb(c & ~DLAB, port + LCR); + + early_serial_base = port; +} + +static void parse_earlyprintk(void) +{ + int baud = DEFAULT_BAUD; + char arg[32]; + int pos = 0; + int port = 0; + + if (cmdline_find_option("earlyprintk", arg, sizeof arg) > 0) { + char *e; + + if (!strncmp(arg, "serial", 6)) { + port = DEFAULT_SERIAL_PORT; + pos += 6; + } + + if (arg[pos] == ',') + pos++; + + if (!strncmp(arg, "ttyS", 4)) { + static const int bases[] = { 0x3f8, 0x2f8 }; + int idx = 0; + + if (!strncmp(arg + pos, "ttyS", 4)) + pos += 4; + + if (arg[pos++] == '1') + idx = 1; + + port = bases[idx]; + } + + if (arg[pos] == ',') + pos++; + + baud = simple_strtoull(arg + pos, &e, 0); + if (baud == 0 || arg + pos == e) + baud = DEFAULT_BAUD; + } + + if (port) + early_serial_init(port, baud); +} + +#define BASE_BAUD (1843200/16) +static unsigned int probe_baud(int port) +{ + unsigned char lcr, dll, dlh; + unsigned int quot; + + lcr = inb(port + LCR); + outb(lcr | DLAB, port + LCR); + dll = inb(port + DLL); + dlh = inb(port + DLH); + outb(lcr, port + LCR); + quot = (dlh << 8) | dll; + + return BASE_BAUD / quot; +} + +static void parse_console_uart8250(void) +{ + char optstr[64], *options; + int baud = DEFAULT_BAUD; + int port = 0; + + /* + * console=uart8250,io,0x3f8,115200n8 + * need to make sure it is last one console ! + */ + if (cmdline_find_option("console", optstr, sizeof optstr) <= 0) + return; + + options = optstr; + + if (!strncmp(options, "uart8250,io,", 12)) + port = simple_strtoull(options + 12, &options, 0); + else if (!strncmp(options, "uart,io,", 8)) + port = simple_strtoull(options + 8, &options, 0); + else + return; + + if (options && (options[0] == ',')) + baud = simple_strtoull(options + 1, &options, 0); + else + baud = probe_baud(port); + + if (port) + early_serial_init(port, baud); +} + +void console_init(void) +{ + parse_earlyprintk(); + + if (!early_serial_base) + parse_console_uart8250(); +} diff --git a/arch/x86/boot/isdigit.h b/arch/x86/boot/isdigit.h new file mode 100644 index 000000000000..25e13403193c --- /dev/null +++ b/arch/x86/boot/isdigit.h @@ -0,0 +1,21 @@ +#ifndef BOOT_ISDIGIT_H + +#define BOOT_ISDIGIT_H + +static inline int isdigit(int ch) +{ + return (ch >= '0') && (ch <= '9'); +} + +static inline int isxdigit(int ch) +{ + if (isdigit(ch)) + return true; + + if ((ch >= 'a') && (ch <= 'f')) + return true; + + return (ch >= 'A') && (ch <= 'F'); +} + +#endif diff --git a/arch/x86/boot/printf.c b/arch/x86/boot/printf.c index 50e47cdbdddd..cdac91ca55d3 100644 --- a/arch/x86/boot/printf.c +++ b/arch/x86/boot/printf.c @@ -34,7 +34,7 @@ static int skip_atoi(const char **s) #define SMALL 32 /* Must be 32 == 0x20 */ #define SPECIAL 64 /* 0x */ -#define do_div(n,base) ({ \ +#define __do_div(n, base) ({ \ int __res; \ __res = ((unsigned long) n) % (unsigned) base; \ n = ((unsigned long) n) / (unsigned) base; \ @@ -83,7 +83,7 @@ static char *number(char *str, long num, int base, int size, int precision, tmp[i++] = '0'; else while (num != 0) - tmp[i++] = (digits[do_div(num, base)] | locase); + tmp[i++] = (digits[__do_div(num, base)] | locase); if (i > precision) precision = i; size -= precision; diff --git a/arch/x86/boot/tty.c b/arch/x86/boot/tty.c index ff4b27a0fc5e..def2451f46ae 100644 --- a/arch/x86/boot/tty.c +++ b/arch/x86/boot/tty.c @@ -15,27 +15,12 @@ #include "boot.h" -#define DEFAULT_SERIAL_PORT 0x3f8 /* ttyS0 */ - -static int early_serial_base; +int early_serial_base; #define XMTRDY 0x20 -#define DLAB 0x80 - #define TXR 0 /* Transmit register (WRITE) */ -#define RXR 0 /* Receive register (READ) */ -#define IER 1 /* Interrupt Enable */ -#define IIR 2 /* Interrupt ID */ -#define FCR 2 /* FIFO control */ -#define LCR 3 /* Line control */ -#define MCR 4 /* Modem control */ #define LSR 5 /* Line Status */ -#define MSR 6 /* Modem Status */ -#define DLL 0 /* Divisor Latch Low */ -#define DLH 1 /* Divisor latch High */ - -#define DEFAULT_BAUD 9600 /* * These functions are in .inittext so they can be used to signal @@ -152,122 +137,3 @@ int getchar_timeout(void) return 0; /* Timeout! */ } -static void early_serial_init(int port, int baud) -{ - unsigned char c; - unsigned divisor; - - outb(0x3, port + LCR); /* 8n1 */ - outb(0, port + IER); /* no interrupt */ - outb(0, port + FCR); /* no fifo */ - outb(0x3, port + MCR); /* DTR + RTS */ - - divisor = 115200 / baud; - c = inb(port + LCR); - outb(c | DLAB, port + LCR); - outb(divisor & 0xff, port + DLL); - outb((divisor >> 8) & 0xff, port + DLH); - outb(c & ~DLAB, port + LCR); - - early_serial_base = port; - - printf("Early serial console at I/O port 0x%x baud: %d\n", port, baud); -} - -static void parse_earlyprintk(void) -{ - int baud = DEFAULT_BAUD; - char arg[32]; - int pos = 0; - int port = 0; - - if (cmdline_find_option("earlyprintk", arg, sizeof arg) > 0) { - char *e; - - if (!strncmp(arg, "serial", 6)) { - port = DEFAULT_SERIAL_PORT; - pos += 6; - } - - if (arg[pos] == ',') - pos++; - - if (!strncmp(arg, "ttyS", 4)) { - static const int bases[] = { 0x3f8, 0x2f8 }; - int idx = 0; - - if (!strncmp(arg + pos, "ttyS", 4)) - pos += 4; - - if (arg[pos++] == '1') - idx = 1; - - port = bases[idx]; - } - - if (arg[pos] == ',') - pos++; - - baud = simple_strtoull(arg + pos, &e, 0); - if (baud == 0 || arg + pos == e) - baud = DEFAULT_BAUD; - } - - if (port) - early_serial_init(port, baud); -} - -#define BASE_BAUD (1843200/16) -static unsigned int probe_baud(int port) -{ - unsigned char lcr, dll, dlh; - unsigned int quot; - - lcr = inb(port + LCR); - outb(lcr | DLAB, port + LCR); - dll = inb(port + DLL); - dlh = inb(port + DLH); - outb(lcr, port + LCR); - quot = (dlh << 8) | dll; - - return BASE_BAUD / quot; -} - -static void parse_console_uart8250(void) -{ - char optstr[64], *options; - int baud = DEFAULT_BAUD; - int port = 0; - - /* - * console=uart8250,io,0x3f8,115200n8 - * need to make sure it is last one console ! - */ - if (cmdline_find_option("console", optstr, sizeof optstr) <= 0) - return; - - options = optstr; - - if (!strncmp(options, "uart8250,io,", 12)) - port = simple_strtoull(options + 12, &options, 0); - else if (!strncmp(options, "uart,io,", 8)) - port = simple_strtoull(options + 8, &options, 0); - else - return; - - if (options && (options[0] == ',')) - baud = simple_strtoull(options + 1, &options, 0); - else - baud = probe_baud(port); - - if (port) - early_serial_init(port, baud); -} - -void console_init(void) -{ - parse_earlyprintk(); - - if (!early_serial_base) - parse_console_uart8250(); -} From 8fee13a48e4879fba57725f6d9513df4bfa8e9f3 Mon Sep 17 00:00:00 2001 From: Yinghai Lu Date: Mon, 2 Aug 2010 16:21:22 -0700 Subject: [PATCH 6/7] x86, setup: enable early console output from the decompressor This enables the decompressor output to be seen on the serial console. Most of the code is shared with the regular boot code. We could add printf to the decompressor if needed, but currently there is no sufficiently compelling user. -v2: define BOOT_BOOT_H to avoid include boot.h -v3: early_serial_base need to be static in misc.c ? -v4: create seperate string.c printf.c cmdline.c early_serial_console.c after hpa's patch that allow global variables in compressed/misc stage -v5: remove printf.c related Signed-off-by: Yinghai Lu Signed-off-by: H. Peter Anvin --- arch/x86/boot/compressed/Makefile | 4 +- arch/x86/boot/compressed/cmdline.c | 21 +++++++ .../boot/compressed/early_serial_console.c | 5 ++ arch/x86/boot/compressed/misc.c | 56 +++++++++++-------- arch/x86/boot/compressed/misc.h | 38 +++++++++++++ arch/x86/boot/compressed/string.c | 4 ++ arch/x86/boot/main.c | 6 +- 7 files changed, 105 insertions(+), 29 deletions(-) create mode 100644 arch/x86/boot/compressed/cmdline.c create mode 100644 arch/x86/boot/compressed/early_serial_console.c create mode 100644 arch/x86/boot/compressed/misc.h create mode 100644 arch/x86/boot/compressed/string.c diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile index fbb47daf2459..0c229551eead 100644 --- a/arch/x86/boot/compressed/Makefile +++ b/arch/x86/boot/compressed/Makefile @@ -4,7 +4,7 @@ # create a compressed vmlinux image from the original vmlinux # -targets := vmlinux.lds vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma vmlinux.bin.lzo head_$(BITS).o misc.o piggy.o +targets := vmlinux.lds vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma vmlinux.bin.lzo head_$(BITS).o misc.o string.o cmdline.o early_serial_console.o piggy.o KBUILD_CFLAGS := -m$(BITS) -D__KERNEL__ $(LINUX_INCLUDE) -O2 KBUILD_CFLAGS += -fno-strict-aliasing -fPIC @@ -23,7 +23,7 @@ LDFLAGS_vmlinux := -T hostprogs-y := mkpiggy -$(obj)/vmlinux: $(obj)/vmlinux.lds $(obj)/head_$(BITS).o $(obj)/misc.o $(obj)/piggy.o FORCE +$(obj)/vmlinux: $(obj)/vmlinux.lds $(obj)/head_$(BITS).o $(obj)/misc.o $(obj)/string.o $(obj)/cmdline.o $(obj)/early_serial_console.o $(obj)/piggy.o FORCE $(call if_changed,ld) @: diff --git a/arch/x86/boot/compressed/cmdline.c b/arch/x86/boot/compressed/cmdline.c new file mode 100644 index 000000000000..cb62f786990d --- /dev/null +++ b/arch/x86/boot/compressed/cmdline.c @@ -0,0 +1,21 @@ +#include "misc.h" + +static unsigned long fs; +static inline void set_fs(unsigned long seg) +{ + fs = seg << 4; /* shift it back */ +} +typedef unsigned long addr_t; +static inline char rdfs8(addr_t addr) +{ + return *((char *)(fs + addr)); +} +#include "../cmdline.c" +int cmdline_find_option(const char *option, char *buffer, int bufsize) +{ + return __cmdline_find_option(real_mode->hdr.cmd_line_ptr, option, buffer, bufsize); +} +int cmdline_find_option_bool(const char *option) +{ + return __cmdline_find_option_bool(real_mode->hdr.cmd_line_ptr, option); +} diff --git a/arch/x86/boot/compressed/early_serial_console.c b/arch/x86/boot/compressed/early_serial_console.c new file mode 100644 index 000000000000..261e81fb9582 --- /dev/null +++ b/arch/x86/boot/compressed/early_serial_console.c @@ -0,0 +1,5 @@ +#include "misc.h" + +int early_serial_base; + +#include "../early_serial_console.c" diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/misc.c index 51e240779a44..8f7bef8e9fff 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -9,23 +9,7 @@ * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996 */ -/* - * we have to be careful, because no indirections are allowed here, and - * paravirt_ops is a kind of one. As it will only run in baremetal anyway, - * we just keep it from happening - */ -#undef CONFIG_PARAVIRT -#ifdef CONFIG_X86_32 -#define _ASM_X86_DESC_H 1 -#endif - -#include -#include -#include -#include -#include -#include -#include +#include "misc.h" /* WARNING!! * This code is compiled with -fPIC and it is relocated dynamically @@ -123,15 +107,13 @@ static void error(char *m); /* * This is set up by the setup-routine at boot-time */ -static struct boot_params *real_mode; /* Pointer to real-mode data */ +struct boot_params *real_mode; /* Pointer to real-mode data */ static int quiet; +static int debug; void *memset(void *s, int c, size_t n); void *memcpy(void *dest, const void *src, size_t n); -static void __putstr(int, const char *); -#define putstr(__x) __putstr(0, __x) - #ifdef CONFIG_X86_64 #define memptr long #else @@ -170,7 +152,21 @@ static void scroll(void) vidmem[i] = ' '; } -static void __putstr(int error, const char *s) +#define XMTRDY 0x20 + +#define TXR 0 /* Transmit register (WRITE) */ +#define LSR 5 /* Line Status */ +static void serial_putchar(int ch) +{ + unsigned timeout = 0xffff; + + while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout) + cpu_relax(); + + outb(ch, early_serial_base + TXR); +} + +void __putstr(int error, const char *s) { int x, y, pos; char c; @@ -179,6 +175,14 @@ static void __putstr(int error, const char *s) if (!error) return; #endif + if (early_serial_base) { + const char *str = s; + while (*str) { + if (*str == '\n') + serial_putchar('\r'); + serial_putchar(*str++); + } + } if (real_mode->screen_info.orig_video_mode == 0 && lines == 0 && cols == 0) @@ -305,8 +309,10 @@ asmlinkage void decompress_kernel(void *rmode, memptr heap, { real_mode = rmode; - if (real_mode->hdr.loadflags & QUIET_FLAG) + if (cmdline_find_option_bool("quiet")) quiet = 1; + if (cmdline_find_option_bool("debug")) + debug = 1; if (real_mode->screen_info.orig_video_mode == 7) { vidmem = (char *) 0xb0000; @@ -319,6 +325,10 @@ asmlinkage void decompress_kernel(void *rmode, memptr heap, lines = real_mode->screen_info.orig_video_lines; cols = real_mode->screen_info.orig_video_cols; + console_init(); + if (debug) + putstr("early console in decompress_kernel\n"); + free_mem_ptr = heap; /* Heap */ free_mem_end_ptr = heap + BOOT_HEAP_SIZE; diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h new file mode 100644 index 000000000000..a267849ac1c9 --- /dev/null +++ b/arch/x86/boot/compressed/misc.h @@ -0,0 +1,38 @@ +#ifndef BOOT_COMPRESSED_MISC_H +#define BOOT_COMPRESSED_MISC_H + +/* + * we have to be careful, because no indirections are allowed here, and + * paravirt_ops is a kind of one. As it will only run in baremetal anyway, + * we just keep it from happening + */ +#undef CONFIG_PARAVIRT +#ifdef CONFIG_X86_32 +#define _ASM_X86_DESC_H 1 +#endif + +#include +#include +#include +#include +#include +#include +#include + +#define BOOT_BOOT_H + +/* misc.c */ +extern struct boot_params *real_mode; /* Pointer to real-mode data */ +void __putstr(int error, const char *s); +#define putstr(__x) __putstr(0, __x) +#define puts(__x) __putstr(0, __x) + +/* cmdline.c */ +int cmdline_find_option(const char *option, char *buffer, int bufsize); +int cmdline_find_option_bool(const char *option); + +/* early_serial_console.c */ +extern int early_serial_base; +void console_init(void); + +#endif diff --git a/arch/x86/boot/compressed/string.c b/arch/x86/boot/compressed/string.c new file mode 100644 index 000000000000..7995c6a49509 --- /dev/null +++ b/arch/x86/boot/compressed/string.c @@ -0,0 +1,4 @@ +#include "misc.h" + +#include "../isdigit.h" +#include "../string.c" diff --git a/arch/x86/boot/main.c b/arch/x86/boot/main.c index 4ef1a33e8572..40358c8905be 100644 --- a/arch/x86/boot/main.c +++ b/arch/x86/boot/main.c @@ -132,6 +132,8 @@ void main(void) /* Initialize the early-boot console */ console_init(); + if (cmdline_find_option_bool("debug")) + puts("early console in setup code\n"); /* End of heap check */ init_heap(); @@ -171,10 +173,6 @@ void main(void) /* Set the video mode */ set_video(); - /* Parse command line for 'quiet' and pass it to decompressor. */ - if (cmdline_find_option_bool("quiet")) - boot_params.hdr.loadflags |= QUIET_FLAG; - /* Do the last things and invoke protected mode */ go_to_protected_mode(); } From 6238b47b58480cd9c092600c05338dbe261b71ce Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Mon, 2 Aug 2010 21:03:46 -0700 Subject: [PATCH 7/7] x86, setup: move isdigit.h to ctype.h, header files on top. It is a subset of functionality, so name it ctype.h. Also, reorganize header files so #include statements are clustered near the top as they should be. Signed-off-by: H. Peter Anvin LKML-Reference: <4C5752F2.8030206@kernel.org> --- arch/x86/boot/boot.h | 3 +-- arch/x86/boot/compressed/misc.h | 1 + arch/x86/boot/compressed/string.c | 2 -- arch/x86/boot/{isdigit.h => ctype.h} | 0 4 files changed, 2 insertions(+), 4 deletions(-) rename arch/x86/boot/{isdigit.h => ctype.h} (100%) diff --git a/arch/x86/boot/boot.h b/arch/x86/boot/boot.h index 00cf51cfc2e6..c7093bd9f2d3 100644 --- a/arch/x86/boot/boot.h +++ b/arch/x86/boot/boot.h @@ -28,6 +28,7 @@ #include "bitops.h" #include #include +#include "ctype.h" /* Useful macros */ #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) @@ -200,8 +201,6 @@ static inline int memcmp_gs(const void *s1, addr_t s2, size_t len) return diff; } -#include "isdigit.h" - /* Heap -- available for dynamic lists. */ extern char _end[]; extern char *HEAP; diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h index a267849ac1c9..3f19c81a6203 100644 --- a/arch/x86/boot/compressed/misc.h +++ b/arch/x86/boot/compressed/misc.h @@ -20,6 +20,7 @@ #include #define BOOT_BOOT_H +#include "../ctype.h" /* misc.c */ extern struct boot_params *real_mode; /* Pointer to real-mode data */ diff --git a/arch/x86/boot/compressed/string.c b/arch/x86/boot/compressed/string.c index 7995c6a49509..19b3e693cd72 100644 --- a/arch/x86/boot/compressed/string.c +++ b/arch/x86/boot/compressed/string.c @@ -1,4 +1,2 @@ #include "misc.h" - -#include "../isdigit.h" #include "../string.c" diff --git a/arch/x86/boot/isdigit.h b/arch/x86/boot/ctype.h similarity index 100% rename from arch/x86/boot/isdigit.h rename to arch/x86/boot/ctype.h