summaryrefslogtreecommitdiff
path: root/com32/lib/syslinux/shuffle_rm.c
blob: a7d16d63db85d327c40ff93aef74fbe3d638b3f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2007-2009 H. Peter Anvin - All Rights Reserved
 *   Copyright 2009 Intel Corporation; author: H. Peter Anvin
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------- */

/*
 * shuffle_rm.c
 *
 * Shuffle and boot to protected mode code
 */

#include <stdlib.h>
#include <inttypes.h>
#include <com32.h>
#include <string.h>
#include <syslinux/movebits.h>
#include <syslinux/bootrm.h>

enum gpr_index { R_AX, R_CX, R_DX, R_BX, R_SP, R_BP, R_SI, R_DI };
enum seg_index { R_ES, R_CS, R_SS, R_DS, R_FS, R_GS };

#define ST8(P,V)						\
  do {								\
    uint8_t *_p = (void *)(P);					\
    *_p++ = (V);						\
    (P) = (void *)_p;						\
  } while (0);
#define ST16(P,V)						\
  do {								\
    uint16_t *_p = (void *)(P);					\
    *_p++ = (V);						\
    (P) = (void *)_p;						\
  } while (0)
#define ST32(P,V)						\
  do {								\
    uint32_t *_p = (void *)(P);					\
    *_p++ = (V);						\
    (P) = (void *)_p;						\
  } while (0)

#define MOV_TO_SEG(P,S,R)					\
    ST16(P, 0xc08e + ((R) << 8) + ((S) << 11))
#define MOV_TO_R16(P,R,V)					\
  do {								\
    ST8(P, 0xb8 + (R));						\
    ST16(P, V);							\
  }  while (0)
#define MOV_TO_R32(P,R,V)					\
  do {								\
    ST16(P, 0xb866 + ((R) << 8));				\
    ST32(P, V);							\
  } while (0)

int syslinux_shuffle_boot_rm(struct syslinux_movelist *fraglist,
			     struct syslinux_memmap *memmap,
			     uint16_t bootflags, struct syslinux_rm_regs *regs)
{
    const struct syslinux_rm_regs_alt {
	uint16_t seg[6];
	uint32_t gpr[8];
	uint32_t csip;
	bool sti;
    } *rp;
    int i, rv;
    uint8_t handoff_code[8 + 5 * 5 + 8 * 6 + 1 + 5], *p;
    uint16_t off;
    struct syslinux_memmap *tmap;
    addr_t regstub, stublen;
    /* Assign GPRs for each sreg, don't use AX and SP */
    static const uint8_t gpr_for_seg[6] =
	{ R_CX, R_DX, R_BX, R_BP, R_SI, R_DI };

    tmap = syslinux_target_memmap(fraglist, memmap);
    if (!tmap)
	return -1;

    /*
     * Search for a good place to put the real-mode register stub.
     * We prefer it as low as possible above 0x800.  KVM barfs horribly
     * if we're not aligned to a paragraph boundary, so set the alignment
     * appropriately.
     */
    regstub = 0x800;
    stublen = sizeof handoff_code;
    rv = syslinux_memmap_find(tmap, SMT_FREE, &regstub, &stublen, 16);

    if (rv || (regstub > 0x100000 - sizeof handoff_code)) {
	/*
	 * Uh-oh.  This isn't real-mode accessible memory.
	 * It might be possible to do something insane here like
	 * putting the stub in the IRQ vectors, or in the 0x5xx segment.
	 * This code tries the 0x510-0x7ff range and hopes for the best.
	 */
	regstub = 0x510;	/* Try the 0x5xx segment... */
	stublen = sizeof handoff_code;
	rv = syslinux_memmap_find(tmap, SMT_FREE, &regstub, &stublen, 16);

	if (!rv && (regstub > 0x100000 - sizeof handoff_code))
	    rv = -1;		/* No acceptable memory found */
    }

    syslinux_free_memmap(tmap);
    if (rv)
	return -1;

    /* Build register-setting stub */
    p = handoff_code;
    rp = (const struct syslinux_rm_regs_alt *)regs;

    /* Set up GPRs with segment registers - don't use AX */
    for (i = 0; i < 6; i++) {
	if (i != R_CS)
	    MOV_TO_R16(p, gpr_for_seg[i], rp->seg[i]);
    }

    /* Actual transition to real mode */
    ST32(p, 0xeac0220f);	/* MOV CR0,EAX; JMP FAR */
    off = (p - handoff_code) + 4;
    ST16(p, off);		/* Offset */
    ST16(p, regstub >> 4);	/* Segment */

    /* Load SS and ESP immediately */
    MOV_TO_SEG(p, R_SS, R_BX);
    MOV_TO_R32(p, R_SP, rp->gpr[R_SP]);

    /* Load the other segments */
    MOV_TO_SEG(p, R_ES, R_CX);
    MOV_TO_SEG(p, R_DS, R_BP);
    MOV_TO_SEG(p, R_FS, R_SI);
    MOV_TO_SEG(p, R_GS, R_DI);

    for (i = 0; i < 8; i++) {
	if (i != R_SP)
	    MOV_TO_R32(p, i, rp->gpr[i]);
    }

    ST8(p, rp->sti ? 0xfb : 0xfa);	/* STI/CLI */

    ST8(p, 0xea);		/* JMP FAR */
    ST32(p, rp->csip);

    /* Add register-setting stub to shuffle list */
    if (syslinux_add_movelist(&fraglist, regstub, (addr_t) handoff_code,
			      sizeof handoff_code))
	return -1;

    return syslinux_do_shuffle(fraglist, memmap, regstub, 0, bootflags);
}