4
4
* SPDX-License-Identifier: BSD-3-Clause
5
5
*/
6
6
7
+ /**
8
+ * @file vcpu.h
9
+ *
10
+ * @brief public APIs for vcpu operations
11
+ */
12
+
7
13
#ifndef VCPU_H
8
14
#define VCPU_H
9
15
48
54
49
55
#include <guest.h>
50
56
57
+ /**
58
+ * @brief vcpu
59
+ *
60
+ * @defgroup acrn_vcpu ACRN vcpu
61
+ * @{
62
+ */
63
+
51
64
enum vcpu_state {
52
65
VCPU_INIT ,
53
66
VCPU_RUNNING ,
@@ -71,12 +84,15 @@ struct segment_sel {
71
84
uint32_t attr ;
72
85
};
73
86
87
+ /**
88
+ * @brief registers info saved for vcpu running context
89
+ */
74
90
struct run_context {
75
91
/* Contains the guest register set.
76
92
* NOTE: This must be the first element in the structure, so that the offsets
77
93
* in vmx_asm.S match
78
94
*/
79
- union {
95
+ union guest_cpu_regs_t {
80
96
struct acrn_gp_regs regs ;
81
97
uint64_t longs [NUM_GPRS ];
82
98
} guest_cpu_regs ;
@@ -266,26 +282,207 @@ vcpu_vlapic(struct vcpu *vcpu)
266
282
}
267
283
268
284
/* External Interfaces */
285
+
286
+ /**
287
+ * @brief get vcpu register value
288
+ *
289
+ * Get target vCPU's general purpose registers value in run_context.
290
+ *
291
+ * @param[in] vcpu pointer to vcpu data structure
292
+ * @param[in] reg register of the vcpu
293
+ *
294
+ * @return the value of the register.
295
+ */
269
296
uint64_t vcpu_get_gpreg (const struct vcpu * vcpu , uint32_t reg );
297
+
298
+ /**
299
+ * @brief set vcpu register value
300
+ *
301
+ * Set target vCPU's general purpose registers value in run_context.
302
+ *
303
+ * @param[inout] vcpu pointer to vcpu data structure
304
+ * @param[in] reg register of the vcpu
305
+ * @param[in] val the value set the register of the vcpu
306
+ */
270
307
void vcpu_set_gpreg (struct vcpu * vcpu , uint32_t reg , uint64_t val );
308
+
309
+ /**
310
+ * @brief get vcpu RIP value
311
+ *
312
+ * Get & cache target vCPU's RIP in run_context.
313
+ *
314
+ * @param[in] vcpu pointer to vcpu data structure
315
+ *
316
+ * @return the value of RIP.
317
+ */
271
318
uint64_t vcpu_get_rip (struct vcpu * vcpu );
319
+
320
+ /**
321
+ * @brief set vcpu RIP value
322
+ *
323
+ * Update target vCPU's RIP in run_context.
324
+ *
325
+ * @param[inout] vcpu pointer to vcpu data structure
326
+ * @param[in] val the value set RIP
327
+ */
272
328
void vcpu_set_rip (struct vcpu * vcpu , uint64_t val );
329
+
330
+ /**
331
+ * @brief get vcpu RSP value
332
+ *
333
+ * Get & cache target vCPU's RSP in run_context.
334
+ *
335
+ * @param[in] vcpu pointer to vcpu data structure
336
+ *
337
+ * @return the value of RSP.
338
+ */
273
339
uint64_t vcpu_get_rsp (struct vcpu * vcpu );
340
+
341
+ /**
342
+ * @brief set vcpu RSP value
343
+ *
344
+ * Update target vCPU's RSP in run_context.
345
+ *
346
+ * @param[inout] vcpu pointer to vcpu data structure
347
+ * @param[in] val the value set RSP
348
+ */
274
349
void vcpu_set_rsp (struct vcpu * vcpu , uint64_t val );
350
+
351
+ /**
352
+ * @brief get vcpu EFER value
353
+ *
354
+ * Get & cache target vCPU's EFER in run_context.
355
+ *
356
+ * @param[in] vcpu pointer to vcpu data structure
357
+ *
358
+ * @return the value of EFER.
359
+ */
275
360
uint64_t vcpu_get_efer (struct vcpu * vcpu );
361
+
362
+ /**
363
+ * @brief set vcpu EFER value
364
+ *
365
+ * Update target vCPU's EFER in run_context.
366
+ *
367
+ * @param[inout] vcpu pointer to vcpu data structure
368
+ * @param[in] val the value set EFER
369
+ */
276
370
void vcpu_set_efer (struct vcpu * vcpu , uint64_t val );
371
+
372
+ /**
373
+ * @brief get vcpu RFLAG value
374
+ *
375
+ * Get & cache target vCPU's RFLAGS in run_context.
376
+ *
377
+ * @param[in] vcpu pointer to vcpu data structure
378
+ *
379
+ * @return the value of RFLAGS.
380
+ */
277
381
uint64_t vcpu_get_rflags (struct vcpu * vcpu );
382
+
383
+ /**
384
+ * @brief set vcpu RFLAGS value
385
+ *
386
+ * Update target vCPU's RFLAGS in run_context.
387
+ *
388
+ * @param[inout] vcpu pointer to vcpu data structure
389
+ * @param[in] val the value set RFLAGS
390
+ */
278
391
void vcpu_set_rflags (struct vcpu * vcpu , uint64_t val );
392
+
393
+ /**
394
+ * @brief get vcpu CR0 value
395
+ *
396
+ * Get & cache target vCPU's CR0 in run_context.
397
+ *
398
+ * @param[in] vcpu pointer to vcpu data structure
399
+ *
400
+ * @return the value of CR0.
401
+ */
279
402
uint64_t vcpu_get_cr0 (struct vcpu * vcpu );
403
+
404
+ /**
405
+ * @brief set vcpu CR0 value
406
+ *
407
+ * Update target vCPU's CR0 in run_context.
408
+ *
409
+ * @param[inout] vcpu pointer to vcpu data structure
410
+ * @param[in] val the value set CR0
411
+ */
280
412
void vcpu_set_cr0 (struct vcpu * vcpu , uint64_t val );
413
+
414
+ /**
415
+ * @brief get vcpu CR2 value
416
+ *
417
+ * Get & cache target vCPU's CR2 in run_context.
418
+ *
419
+ * @param[in] vcpu pointer to vcpu data structure
420
+ *
421
+ * @return the value of CR2.
422
+ */
281
423
uint64_t vcpu_get_cr2 (struct vcpu * vcpu );
424
+
425
+ /**
426
+ * @brief set vcpu CR2 value
427
+ *
428
+ * Update target vCPU's CR2 in run_context.
429
+ *
430
+ * @param[inout] vcpu pointer to vcpu data structure
431
+ * @param[in] val the value set CR2
432
+ */
282
433
void vcpu_set_cr2 (struct vcpu * vcpu , uint64_t val );
434
+
435
+ /**
436
+ * @brief get vcpu CR4 value
437
+ *
438
+ * Get & cache target vCPU's CR4 in run_context.
439
+ *
440
+ * @param[in] vcpu pointer to vcpu data structure
441
+ *
442
+ * @return the value of CR4.
443
+ */
283
444
uint64_t vcpu_get_cr4 (struct vcpu * vcpu );
445
+
446
+ /**
447
+ * @brief set vcpu CR4 value
448
+ *
449
+ * Update target vCPU's CR4 in run_context.
450
+ *
451
+ * @param[inout] vcpu pointer to vcpu data structure
452
+ * @param[in] val the value set CR4
453
+ */
284
454
void vcpu_set_cr4 (struct vcpu * vcpu , uint64_t val );
455
+
285
456
uint64_t vcpu_get_pat_ext (const struct vcpu * vcpu );
286
457
void vcpu_set_pat_ext (struct vcpu * vcpu , uint64_t val );
458
+
459
+ /**
460
+ * @brief set all the vcpu registers
461
+ *
462
+ * Update target vCPU's all registers in run_context.
463
+ *
464
+ * @param[inout] vcpu pointer to vcpu data structure
465
+ * @param[in] vcpu_regs all the registers' value
466
+ */
287
467
void set_vcpu_regs (struct vcpu * vcpu , struct acrn_vcpu_regs * vcpu_regs );
468
+
469
+ /**
470
+ * @brief reset all the vcpu registers
471
+ *
472
+ * Reset target vCPU's all registers in run_context to initial values.
473
+ *
474
+ * @param[inout] vcpu pointer to vcpu data structure
475
+ */
288
476
void reset_vcpu_regs (struct vcpu * vcpu );
477
+
478
+ /**
479
+ * @brief set the vcpu AP entry
480
+ *
481
+ * Set target vCPU's AP running entry in run_context.
482
+ *
483
+ * @param[inout] vcpu pointer to vcpu data structure
484
+ * @param[in] entry the entry value for AP
485
+ */
289
486
void set_ap_entry (struct vcpu * vcpu , uint64_t entry );
290
487
291
488
static inline bool is_long_mode (struct vcpu * vcpu )
@@ -304,26 +501,102 @@ static inline bool is_pae(struct vcpu *vcpu)
304
501
}
305
502
306
503
struct vcpu * get_ever_run_vcpu (uint16_t pcpu_id );
504
+
505
+ /**
506
+ * @brief create a vcpu for the target vm
507
+ *
508
+ * Creates/allocates a vCPU instance, with initialization for its vcpu_id,
509
+ * vpid, vmcs, vlapic, etc. It sets the init vCPU state to VCPU_INIT
510
+ *
511
+ * @param[in] pcpu_id created vcpu will run on this pcpu
512
+ * @param[in] vm pointer to vm data structure, this vcpu will owned by this vm.
513
+ * @param[out] rtn_vcpu_handle pointer to the created vcpu
514
+ *
515
+ * @return 0: vcpu created successfully, other values failed.
516
+ */
307
517
int create_vcpu (uint16_t pcpu_id , struct vm * vm , struct vcpu * * rtn_vcpu_handle );
308
- /*
309
- * @pre vcpu != NULL
518
+
519
+ /**
520
+ * @brief run into non-root mode based on vcpu setting
521
+ *
522
+ * An interface in vCPU thread to implement VM entry and VM exit.
523
+ * A CPU switches between VMX root mode and non-root mode based on it.
524
+ *
525
+ * @param[inout] vcpu pointer to vcpu data structure
526
+ * @pre vcpu != NULL
527
+ *
528
+ * @return 0: vcpu run successfully, other values failed.
310
529
*/
311
530
int run_vcpu (struct vcpu * vcpu );
531
+
312
532
int shutdown_vcpu (struct vcpu * vcpu );
313
- /*
314
- * @pre vcpu != NULL
533
+
534
+ /**
535
+ * @brief unmap the vcpu with pcpu and free its vlapic
536
+ *
537
+ * Unmap the vcpu with pcpu and free its vlapic, and set the vcpu state to offline
538
+ *
539
+ * @param[inout] vcpu pointer to vcpu data structure
540
+ * @pre vcpu != NULL
315
541
*/
316
542
void offline_vcpu (struct vcpu * vcpu );
317
543
544
+ /**
545
+ * @brief reset vcpu state and values
546
+ *
547
+ * Reset all fields in a vCPU instance, the vCPU state is reset to VCPU_INIT.
548
+ *
549
+ * @param[inout] vcpu pointer to vcpu data structure
550
+ */
318
551
void reset_vcpu (struct vcpu * vcpu );
552
+
553
+ /**
554
+ * @brief pause the vcpu and set new state
555
+ *
556
+ * Change a vCPU state to VCPU_PAUSED or VCPU_ZOMBIE, and make a reschedule request for it.
557
+ *
558
+ * @param[inout] vcpu pointer to vcpu data structure
559
+ * @param[in] new_state the state to set vcpu
560
+ */
319
561
void pause_vcpu (struct vcpu * vcpu , enum vcpu_state new_state );
562
+
563
+ /**
564
+ * @brief resume the vcpu
565
+ *
566
+ * Change a vCPU state to VCPU_RUNNING, and make a reschedule request for it.
567
+ *
568
+ * @param[inout] vcpu pointer to vcpu data structure
569
+ */
320
570
void resume_vcpu (struct vcpu * vcpu );
571
+
572
+ /**
573
+ * @brief set the vcpu to running state, then it will be scheculed.
574
+ *
575
+ * Adds a vCPU into the run queue and make a reschedule request for it. It sets the vCPU state to VCPU_RUNNING.
576
+ *
577
+ * @param[inout] vcpu pointer to vcpu data structure
578
+ */
321
579
void schedule_vcpu (struct vcpu * vcpu );
580
+
581
+ /**
582
+ * @brief create a vcpu for the vm and mapped to the pcpu.
583
+ *
584
+ * Create a vcpu for the vm, and mapped to the pcpu.
585
+ *
586
+ * @param[inout] vm pointer to vm data structure
587
+ * @param[in] pcpu_id which the vcpu will be mapped
588
+ */
322
589
int prepare_vcpu (struct vm * vm , uint16_t pcpu_id );
323
590
324
591
void request_vcpu_pre_work (struct vcpu * vcpu , uint16_t pre_work_id );
325
592
326
593
void vcpu_dumpreg (void * data );
594
+
595
+ /**
596
+ * @}
597
+ */
598
+ /* End of acrn_vcpu */
599
+
327
600
#endif /* ASSEMBLER */
328
601
329
602
#endif /* VCPU_H */
0 commit comments