Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / interface / pxe / pxe_call.c
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <ipxe/uaccess.h>
23 #include <ipxe/init.h>
24 #include <ipxe/profile.h>
25 #include <setjmp.h>
26 #include <registers.h>
27 #include <biosint.h>
28 #include <pxe.h>
29 #include <pxe_call.h>
30
31 /** @file
32  *
33  * PXE API entry point
34  */
35
36 /* Disambiguate the various error causes */
37 #define EINFO_EPXENBP                                                   \
38         __einfo_uniqify ( EINFO_EPLATFORM, 0x01,                        \
39                           "External PXE NBP error" )
40 #define EPXENBP( status ) EPLATFORM ( EINFO_EPXENBP, status )
41
42 /** Vector for chaining INT 1A */
43 extern struct segoff __text16 ( pxe_int_1a_vector );
44 #define pxe_int_1a_vector __use_text16 ( pxe_int_1a_vector )
45
46 /** INT 1A handler */
47 extern void pxe_int_1a ( void );
48
49 /** INT 1A hooked flag */
50 static int int_1a_hooked = 0;
51
52 /** PXENV_UNDI_TRANSMIT API call profiler */
53 static struct profiler pxe_api_tx_profiler __profiler =
54         { .name = "pxeapi.tx" };
55
56 /** PXENV_UNDI_ISR API call profiler */
57 static struct profiler pxe_api_isr_profiler __profiler =
58         { .name = "pxeapi.isr" };
59
60 /** PXE unknown API call profiler
61  *
62  * This profiler can be used to measure the overhead of a dummy PXE
63  * API call.
64  */
65 static struct profiler pxe_api_unknown_profiler __profiler =
66         { .name = "pxeapi.unknown" };
67
68 /** Miscellaneous PXE API call profiler */
69 static struct profiler pxe_api_misc_profiler __profiler =
70         { .name = "pxeapi.misc" };
71
72 /**
73  * Handle an unknown PXE API call
74  *
75  * @v pxenv_unknown                     Pointer to a struct s_PXENV_UNKNOWN
76  * @ret #PXENV_EXIT_FAILURE             Always
77  * @err #PXENV_STATUS_UNSUPPORTED       Always
78  */
79 static PXENV_EXIT_t pxenv_unknown ( struct s_PXENV_UNKNOWN *pxenv_unknown ) {
80         pxenv_unknown->Status = PXENV_STATUS_UNSUPPORTED;
81         return PXENV_EXIT_FAILURE;
82 }
83
84 /** Unknown PXE API call list */
85 struct pxe_api_call pxenv_unknown_api __pxe_api_call =
86         PXE_API_CALL ( PXENV_UNKNOWN, pxenv_unknown, struct s_PXENV_UNKNOWN );
87
88 /**
89  * Locate PXE API call
90  *
91  * @v opcode            Opcode
92  * @ret call            PXE API call, or NULL
93  */
94 static struct pxe_api_call * find_pxe_api_call ( uint16_t opcode ) {
95         struct pxe_api_call *call;
96
97         for_each_table_entry ( call, PXE_API_CALLS ) {
98                 if ( call->opcode == opcode )
99                         return call;
100         }
101         return NULL;
102 }
103
104 /**
105  * Determine applicable profiler (for debugging)
106  *
107  * @v opcode            PXE opcode
108  * @ret profiler        Profiler
109  */
110 static struct profiler * pxe_api_profiler ( unsigned int opcode ) {
111
112         /* Determine applicable profiler */
113         switch ( opcode ) {
114         case PXENV_UNDI_TRANSMIT:
115                 return &pxe_api_tx_profiler;
116         case PXENV_UNDI_ISR:
117                 return &pxe_api_isr_profiler;
118         case PXENV_UNKNOWN:
119                 return &pxe_api_unknown_profiler;
120         default:
121                 return &pxe_api_misc_profiler;
122         }
123 }
124
125 /**
126  * Dispatch PXE API call
127  *
128  * @v bx                PXE opcode
129  * @v es:di             Address of PXE parameter block
130  * @ret ax              PXE exit code
131  */
132 __asmcall void pxe_api_call ( struct i386_all_regs *ix86 ) {
133         uint16_t opcode = ix86->regs.bx;
134         userptr_t uparams = real_to_user ( ix86->segs.es, ix86->regs.di );
135         struct profiler *profiler = pxe_api_profiler ( opcode );
136         struct pxe_api_call *call;
137         union u_PXENV_ANY params;
138         PXENV_EXIT_t ret;
139
140         /* Start profiling */
141         profile_start ( profiler );
142
143         /* Locate API call */
144         call = find_pxe_api_call ( opcode );
145         if ( ! call ) {
146                 DBGC ( &pxe_netdev, "PXENV_UNKNOWN_%04x\n", opcode );
147                 call = &pxenv_unknown_api;
148         }
149
150         /* Copy parameter block from caller */
151         copy_from_user ( &params, uparams, 0, call->params_len );
152
153         /* Set default status in case child routine fails to do so */
154         params.Status = PXENV_STATUS_FAILURE;
155
156         /* Hand off to relevant API routine */
157         ret = call->entry ( &params );
158
159         /* Copy modified parameter block back to caller and return */
160         copy_to_user ( uparams, 0, &params, call->params_len );
161         ix86->regs.ax = ret;
162
163         /* Stop profiling, if applicable */
164         profile_stop ( profiler );
165 }
166
167 /**
168  * Dispatch weak PXE API call with PXE stack available
169  *
170  * @v ix86              Registers for PXE call
171  * @ret present         Zero (PXE stack present)
172  */
173 int pxe_api_call_weak ( struct i386_all_regs *ix86 ) {
174         pxe_api_call ( ix86 );
175         return 0;
176 }
177
178 /**
179  * Dispatch PXE loader call
180  *
181  * @v es:di             Address of PXE parameter block
182  * @ret ax              PXE exit code
183  */
184 __asmcall void pxe_loader_call ( struct i386_all_regs *ix86 ) {
185         userptr_t uparams = real_to_user ( ix86->segs.es, ix86->regs.di );
186         struct s_UNDI_LOADER params;
187         PXENV_EXIT_t ret;
188
189         /* Copy parameter block from caller */
190         copy_from_user ( &params, uparams, 0, sizeof ( params ) );
191
192         /* Fill in ROM segment address */
193         ppxe.UNDIROMID.segment = ix86->segs.ds;
194
195         /* Set default status in case child routine fails to do so */
196         params.Status = PXENV_STATUS_FAILURE;
197
198         /* Call UNDI loader */
199         ret = undi_loader ( &params );
200
201         /* Copy modified parameter block back to caller and return */
202         copy_to_user ( uparams, 0, &params, sizeof ( params ) );
203         ix86->regs.ax = ret;
204 }
205
206 /**
207  * Calculate byte checksum as used by PXE
208  *
209  * @v data              Data
210  * @v size              Length of data
211  * @ret sum             Checksum
212  */
213 static uint8_t pxe_checksum ( void *data, size_t size ) {
214         uint8_t *bytes = data;
215         uint8_t sum = 0;
216
217         while ( size-- ) {
218                 sum += *bytes++;
219         }
220         return sum;
221 }
222
223 /**
224  * Initialise !PXE and PXENV+ structures
225  *
226  */
227 static void pxe_init_structures ( void ) {
228         uint32_t rm_cs_phys = ( rm_cs << 4 );
229         uint32_t rm_ds_phys = ( rm_ds << 4 );
230
231         /* Fill in missing segment fields */
232         ppxe.EntryPointSP.segment = rm_cs;
233         ppxe.EntryPointESP.segment = rm_cs;
234         ppxe.Stack.segment_address = rm_ds;
235         ppxe.Stack.Physical_address = rm_ds_phys;
236         ppxe.UNDIData.segment_address = rm_ds;
237         ppxe.UNDIData.Physical_address = rm_ds_phys;
238         ppxe.UNDICode.segment_address = rm_cs;
239         ppxe.UNDICode.Physical_address = rm_cs_phys;
240         ppxe.UNDICodeWrite.segment_address = rm_cs;
241         ppxe.UNDICodeWrite.Physical_address = rm_cs_phys;
242         pxenv.RMEntry.segment = rm_cs;
243         pxenv.StackSeg = rm_ds;
244         pxenv.UNDIDataSeg = rm_ds;
245         pxenv.UNDICodeSeg = rm_cs;
246         pxenv.PXEPtr.segment = rm_cs;
247
248         /* Update checksums */
249         ppxe.StructCksum -= pxe_checksum ( &ppxe, sizeof ( ppxe ) );
250         pxenv.Checksum -= pxe_checksum ( &pxenv, sizeof ( pxenv ) );
251 }
252
253 /** PXE structure initialiser */
254 struct init_fn pxe_init_fn __init_fn ( INIT_NORMAL ) = {
255         .initialise = pxe_init_structures,
256 };
257
258 /**
259  * Activate PXE stack
260  *
261  * @v netdev            Net device to use as PXE net device
262  */
263 void pxe_activate ( struct net_device *netdev ) {
264
265         /* Ensure INT 1A is hooked */
266         if ( ! int_1a_hooked ) {
267                 hook_bios_interrupt ( 0x1a, ( unsigned int ) pxe_int_1a,
268                                       &pxe_int_1a_vector );
269                 devices_get();
270                 int_1a_hooked = 1;
271         }
272
273         /* Set PXE network device */
274         pxe_set_netdev ( netdev );
275 }
276
277 /**
278  * Deactivate PXE stack
279  *
280  * @ret rc              Return status code
281  */
282 int pxe_deactivate ( void ) {
283         int rc;
284
285         /* Clear PXE network device */
286         pxe_set_netdev ( NULL );
287
288         /* Ensure INT 1A is unhooked, if possible */
289         if ( int_1a_hooked ) {
290                 if ( ( rc = unhook_bios_interrupt ( 0x1a,
291                                                     (unsigned int) pxe_int_1a,
292                                                     &pxe_int_1a_vector ))!= 0){
293                         DBG ( "Could not unhook INT 1A: %s\n",
294                               strerror ( rc ) );
295                         return rc;
296                 }
297                 devices_put();
298                 int_1a_hooked = 0;
299         }
300
301         return 0;
302 }
303
304 /** Jump buffer for PXENV_RESTART_TFTP */
305 rmjmp_buf pxe_restart_nbp;
306
307 /**
308  * Start PXE NBP at 0000:7c00
309  *
310  * @ret rc              Return status code
311  */
312 int pxe_start_nbp ( void ) {
313         int jmp;
314         int discard_b, discard_c, discard_d, discard_D;
315         uint16_t status;
316
317         /* Allow restarting NBP via PXENV_RESTART_TFTP */
318         jmp = rmsetjmp ( pxe_restart_nbp );
319         if ( jmp )
320                 DBG ( "Restarting NBP (%x)\n", jmp );
321
322         /* Far call to PXE NBP */
323         __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
324                                            "movw %%cx, %%es\n\t"
325                                            "pushw %%es\n\t"
326                                            "pushw %%di\n\t"
327                                            "sti\n\t"
328                                            "lcall $0, $0x7c00\n\t"
329                                            "popl %%ebp\n\t" /* discard */
330                                            "popl %%ebp\n\t" /* gcc bug */ )
331                                : "=a" ( status ), "=b" ( discard_b ),
332                                  "=c" ( discard_c ), "=d" ( discard_d ),
333                                  "=D" ( discard_D )
334                                : "a" ( 0 ), "b" ( __from_text16 ( &pxenv ) ),
335                                  "c" ( rm_cs ),
336                                  "d" ( virt_to_phys ( &pxenv ) ),
337                                  "D" ( __from_text16 ( &ppxe ) )
338                                : "esi", "memory" );
339         if ( status )
340                 return -EPXENBP ( status );
341
342         return 0;
343 }
344
345 REQUIRE_OBJECT ( pxe_preboot );
346 REQUIRE_OBJECT ( pxe_undi );
347 REQUIRE_OBJECT ( pxe_udp );
348 REQUIRE_OBJECT ( pxe_tftp );
349 REQUIRE_OBJECT ( pxe_file );