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