Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1 | #include <console/console.h> |
Stefan Reinauer | e8b08ba | 2013-05-24 15:09:36 -0700 | [diff] [blame] | 2 | #include <arch/stages.h> |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 3 | #include <program_loading.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 4 | #include <ip_checksum.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 5 | #include <string.h> |
Julius Werner | ec5e5e0 | 2014-08-20 15:29:56 -0700 | [diff] [blame^] | 6 | #include <symbols.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 7 | |
Aaron Durbin | 8e4a355 | 2013-02-08 17:28:04 -0600 | [diff] [blame] | 8 | /* When the ramstage is relocatable the elf loading ensures an elf image cannot |
| 9 | * be loaded over the ramstage code. */ |
Aaron Durbin | 7d1996c | 2014-02-24 22:27:39 -0600 | [diff] [blame] | 10 | static void jmp_payload_no_bounce_buffer(void *entry) |
Aaron Durbin | 8e4a355 | 2013-02-08 17:28:04 -0600 | [diff] [blame] | 11 | { |
Aaron Durbin | 8e4a355 | 2013-02-08 17:28:04 -0600 | [diff] [blame] | 12 | /* Jump to kernel */ |
| 13 | __asm__ __volatile__( |
| 14 | " cld \n\t" |
| 15 | /* Now jump to the loaded image */ |
| 16 | " call *%0\n\t" |
| 17 | |
| 18 | /* The loaded image returned? */ |
| 19 | " cli \n\t" |
| 20 | " cld \n\t" |
| 21 | |
| 22 | :: |
Aaron Durbin | 7ae7fc0 | 2013-05-28 16:15:01 -0500 | [diff] [blame] | 23 | "r" (entry) |
Aaron Durbin | 8e4a355 | 2013-02-08 17:28:04 -0600 | [diff] [blame] | 24 | ); |
| 25 | } |
Aaron Durbin | 7d1996c | 2014-02-24 22:27:39 -0600 | [diff] [blame] | 26 | |
| 27 | static void jmp_payload(void *entry, unsigned long buffer, unsigned long size) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 28 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 29 | unsigned long lb_start, lb_size; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 30 | |
Julius Werner | ec5e5e0 | 2014-08-20 15:29:56 -0700 | [diff] [blame^] | 31 | lb_start = (unsigned long)&_program; |
| 32 | lb_size = _program_size; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 33 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 34 | printk(BIOS_SPEW, "entry = 0x%08lx\n", (unsigned long)entry); |
| 35 | printk(BIOS_SPEW, "lb_start = 0x%08lx\n", lb_start); |
| 36 | printk(BIOS_SPEW, "lb_size = 0x%08lx\n", lb_size); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 37 | printk(BIOS_SPEW, "buffer = 0x%08lx\n", buffer); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 38 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 39 | /* Jump to kernel */ |
| 40 | __asm__ __volatile__( |
| 41 | " cld \n\t" |
| 42 | /* Save the callee save registers... */ |
| 43 | " pushl %%esi\n\t" |
| 44 | " pushl %%edi\n\t" |
| 45 | " pushl %%ebx\n\t" |
| 46 | /* Save the parameters I was passed */ |
| 47 | " pushl $0\n\t" /* 20 adjust */ |
| 48 | " pushl %0\n\t" /* 16 lb_start */ |
| 49 | " pushl %1\n\t" /* 12 buffer */ |
| 50 | " pushl %2\n\t" /* 8 lb_size */ |
| 51 | " pushl %3\n\t" /* 4 entry */ |
| 52 | " pushl %4\n\t" /* 0 elf_boot_notes */ |
| 53 | /* Compute the adjustment */ |
| 54 | " xorl %%eax, %%eax\n\t" |
| 55 | " subl 16(%%esp), %%eax\n\t" |
| 56 | " addl 12(%%esp), %%eax\n\t" |
| 57 | " addl 8(%%esp), %%eax\n\t" |
| 58 | " movl %%eax, 20(%%esp)\n\t" |
Stefan Reinauer | f834e20 | 2009-03-31 17:17:30 +0000 | [diff] [blame] | 59 | /* Place a copy of coreboot in its new location */ |
Stefan Reinauer | f8ee180 | 2008-01-18 15:08:58 +0000 | [diff] [blame] | 60 | /* Move ``longs'' the coreboot size is 4 byte aligned */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 61 | " movl 12(%%esp), %%edi\n\t" |
| 62 | " addl 8(%%esp), %%edi\n\t" |
| 63 | " movl 16(%%esp), %%esi\n\t" |
| 64 | " movl 8(%%esp), %%ecx\n\n" |
| 65 | " shrl $2, %%ecx\n\t" |
| 66 | " rep movsl\n\t" |
| 67 | |
Stefan Reinauer | f8ee180 | 2008-01-18 15:08:58 +0000 | [diff] [blame] | 68 | /* Adjust the stack pointer to point into the new coreboot image */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 69 | " addl 20(%%esp), %%esp\n\t" |
Stefan Reinauer | f8ee180 | 2008-01-18 15:08:58 +0000 | [diff] [blame] | 70 | /* Adjust the instruction pointer to point into the new coreboot image */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 71 | " movl $1f, %%eax\n\t" |
| 72 | " addl 20(%%esp), %%eax\n\t" |
| 73 | " jmp *%%eax\n\t" |
| 74 | "1: \n\t" |
| 75 | |
Stefan Reinauer | f8ee180 | 2008-01-18 15:08:58 +0000 | [diff] [blame] | 76 | /* Copy the coreboot bounce buffer over coreboot */ |
| 77 | /* Move ``longs'' the coreboot size is 4 byte aligned */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 78 | " movl 16(%%esp), %%edi\n\t" |
| 79 | " movl 12(%%esp), %%esi\n\t" |
| 80 | " movl 8(%%esp), %%ecx\n\t" |
| 81 | " shrl $2, %%ecx\n\t" |
| 82 | " rep movsl\n\t" |
| 83 | |
| 84 | /* Now jump to the loaded image */ |
Robert Millan | 81af3d4 | 2008-11-11 20:20:54 +0000 | [diff] [blame] | 85 | " movl %5, %%eax\n\t" |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 86 | " movl 0(%%esp), %%ebx\n\t" |
| 87 | " call *4(%%esp)\n\t" |
| 88 | |
| 89 | /* The loaded image returned? */ |
| 90 | " cli \n\t" |
| 91 | " cld \n\t" |
| 92 | |
Stefan Reinauer | f8ee180 | 2008-01-18 15:08:58 +0000 | [diff] [blame] | 93 | /* Copy the saved copy of coreboot where coreboot runs */ |
| 94 | /* Move ``longs'' the coreboot size is 4 byte aligned */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 95 | " movl 16(%%esp), %%edi\n\t" |
| 96 | " movl 12(%%esp), %%esi\n\t" |
| 97 | " addl 8(%%esp), %%esi\n\t" |
| 98 | " movl 8(%%esp), %%ecx\n\t" |
| 99 | " shrl $2, %%ecx\n\t" |
| 100 | " rep movsl\n\t" |
| 101 | |
Stefan Reinauer | f8ee180 | 2008-01-18 15:08:58 +0000 | [diff] [blame] | 102 | /* Adjust the stack pointer to point into the old coreboot image */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 103 | " subl 20(%%esp), %%esp\n\t" |
| 104 | |
Stefan Reinauer | f8ee180 | 2008-01-18 15:08:58 +0000 | [diff] [blame] | 105 | /* Adjust the instruction pointer to point into the old coreboot image */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 106 | " movl $1f, %%eax\n\t" |
| 107 | " subl 20(%%esp), %%eax\n\t" |
| 108 | " jmp *%%eax\n\t" |
| 109 | "1: \n\t" |
| 110 | |
| 111 | /* Drop the parameters I was passed */ |
| 112 | " addl $24, %%esp\n\t" |
| 113 | |
| 114 | /* Restore the callee save registers */ |
| 115 | " popl %%ebx\n\t" |
| 116 | " popl %%edi\n\t" |
| 117 | " popl %%esi\n\t" |
| 118 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 119 | :: |
Myles Watson | 2a63ea5 | 2009-03-20 18:29:49 +0000 | [diff] [blame] | 120 | "ri" (lb_start), "ri" (buffer), "ri" (lb_size), |
| 121 | "ri" (entry), |
Stefan Reinauer | e8b08ba | 2013-05-24 15:09:36 -0700 | [diff] [blame] | 122 | "ri"(0), "ri" (0) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 123 | ); |
| 124 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 125 | |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 126 | static void try_payload(struct prog *prog) |
Aaron Durbin | 7d1996c | 2014-02-24 22:27:39 -0600 | [diff] [blame] | 127 | { |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 128 | if (prog->type == PROG_PAYLOAD) { |
| 129 | if (IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) |
| 130 | jmp_payload_no_bounce_buffer(prog_entry(prog)); |
| 131 | else |
| 132 | jmp_payload(prog_entry(prog), |
| 133 | (uintptr_t)prog_start(prog), |
| 134 | prog_size(prog)); |
| 135 | } |
Aaron Durbin | 7d1996c | 2014-02-24 22:27:39 -0600 | [diff] [blame] | 136 | } |
Aaron Durbin | b3847e6 | 2015-03-20 15:55:08 -0500 | [diff] [blame] | 137 | |
| 138 | void arch_prog_run(struct prog *prog) |
| 139 | { |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 140 | if (ENV_RAMSTAGE) |
| 141 | try_payload(prog); |
Aaron Durbin | b3847e6 | 2015-03-20 15:55:08 -0500 | [diff] [blame] | 142 | __asm__ volatile ( |
| 143 | "jmp *%%edi\n" |
| 144 | :: "D"(prog_entry(prog)) |
| 145 | ); |
| 146 | } |