Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 1 | # Scheduler VM expectations |
| 2 | |
| 3 | Hafnium requires there to be a special 'primary' or 'scheduler' VM which is |
| 4 | responsible for scheduling the other VMs. There are some particular expectations |
| 5 | on this VM that are required for the rest of the system to function normally. |
| 6 | |
Andrew Walbran | b784997 | 2019-11-15 15:23:43 +0000 | [diff] [blame] | 7 | [TOC] |
| 8 | |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 9 | ## Scheduling |
| 10 | |
| 11 | The scheduler VM is responsible for scheduling the vCPUs of all the other VMs. |
| 12 | It should request information about the VMs in the system using the |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 13 | `FFA_PARTITION_INFO_GET` function, and then schedule their vCPUs as it wishes. |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 14 | The recommended way of doing this is to create a kernel thread for each vCPU, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 15 | which will repeatedly run that vCPU by calling `FFA_RUN`. |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 16 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 17 | `FFA_RUN` will return one of several possible functions, which must be handled |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 18 | as follows: |
| 19 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 20 | ### `FFA_INTERRUPT` |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 21 | |
| 22 | The vCPU has been preempted but still has work to do. If the scheduling quantum |
| 23 | has not expired, the scheduler MUST call `hf_vcpu_run` on the vCPU to allow it |
| 24 | to continue. |
| 25 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 26 | ### `FFA_YIELD` |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 27 | |
| 28 | The vCPU has voluntarily yielded the CPU. The scheduler SHOULD take a scheduling |
| 29 | decision to give cycles to those that need them but MUST call `hf_vcpu_run` on |
| 30 | the vCPU at a later point. |
| 31 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 32 | ### `FFA_MSG_WAIT` |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 33 | |
| 34 | The vCPU is blocked waiting for a message. The scheduler MUST take it off the |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 35 | run queue and not call `FFA_RUN` on the vCPU until it has either: |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 36 | |
| 37 | * injected an interrupt |
| 38 | * sent it a message |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 39 | * received `HF_FFA_RUN_WAKE_UP` for it from another vCPU |
| 40 | * the timeout provided in `w2` is not `FFA_SLEEP_INDEFINITE` and the |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 41 | specified duration has expired. |
| 42 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 43 | ### `FFA_MSG_SEND` |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 44 | |
| 45 | A message has been sent by the vCPU. If the recipient is the scheduler VM itself |
| 46 | then it can handle it as it pleases. Otherwise the scheduler MUST run a vCPU |
| 47 | from the recipient VM and priority SHOULD be given to those vCPUs that are |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 48 | waiting for a message. The scheduler should call FFA_RUN again on the sending |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 49 | VM as usual. |
| 50 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 51 | ### `FFA_RX_RELEASE` |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 52 | |
| 53 | The vCPU has made the mailbox writable and there are pending waiters. The |
| 54 | scheduler MUST call `hf_mailbox_waiter_get()` repeatedly and notify all waiters |
| 55 | by injecting an `HF_MAILBOX_WRITABLE_INTID` interrupt. The scheduler should call |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 56 | FFA_RUN again on the sending VM as usual. |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 57 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 58 | ### `HF_FFA_RUN_WAIT_FOR_INTERRUPT` |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 59 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 60 | _This is a Hafnium-specific function not part of the FF-A standard._ |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 61 | |
| 62 | The vCPU is blocked waiting for an interrupt. The scheduler MUST take it off the |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 63 | run queue and not call `FFA_RUN` on the vCPU until it has either: |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 64 | |
| 65 | * injected an interrupt |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 66 | * received `HF_FFA_RUN_WAKE_UP` for it from another vCPU |
| 67 | * the timeout provided in `w2` is not `FFA_SLEEP_INDEFINITE` and the |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 68 | specified duration has expired. |
| 69 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 70 | ### `HF_FFA_RUN_WAKE_UP` |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 71 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 72 | _This is a Hafnium-specific function not part of the FF-A standard._ |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 73 | |
| 74 | Hafnium would like `hf_vcpu_run` to be called on another vCPU, specified by |
| 75 | `hf_vcpu_run_return.wake_up`. The scheduler MUST either wake the vCPU in |
| 76 | question up if it is blocked, or preempt and re-run it if it is already running |
| 77 | somewhere. This gives Hafnium a chance to update any CPU state which might have |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 78 | changed. The scheduler should call FFA_RUN again on the sending VM as usual. |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 79 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 80 | ### `FFA_ERROR` |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 81 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 82 | #### `FFA_ABORTED` |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 83 | |
| 84 | The vCPU has aborted triggering the whole VM to abort. The scheduler MUST treat |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 85 | this the same as `HF_FFA_RUN_WAKE_UP` for all the other vCPUs of the VM. For |
| 86 | this vCPU the scheduler SHOULD either never call FFA_RUN on the vCPU again, or |
| 87 | treat it the same as `HF_FFA_RUN_WAIT_FOR_INTERRUPT`. |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 88 | |
| 89 | #### Any other error code |
| 90 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 91 | This should not happen if the scheduler VM has called `FFA_RUN` correctly, but |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 92 | in case there is some other error it should be logged. The scheduler SHOULD |
| 93 | either try again or suspend the vCPU indefinitely. |
| 94 | |
| 95 | ## Interrupt handling |
| 96 | |
| 97 | The scheduler VM is responsible for handling all hardware interrupts. Many of |
| 98 | these will be intended for the scheduler VM itself and it can handle them as |
| 99 | usual. However, it must also: |
| 100 | |
| 101 | * Enable, handle and ignore interrupts for the non-secure hypervisor physical |
| 102 | timer (PPI 10, IRQ 26). |
| 103 | * Forward interrupts intended for secondary VMs to an appropriate vCPU of the |
| 104 | VM by calling `hf_interrupt_inject` and then running the vCPU as usual with |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 105 | `FFA_RUN`. (If the vCPU is already running at the time that |
Andrew Walbran | 6e524d7 | 2019-11-12 17:36:57 +0000 | [diff] [blame] | 106 | `hf_interrupt_inject` is called then it must be preempted and run again so |
| 107 | that Hafnium can inject the interrupt.) |