1/**
2 * \file
3 * \brief Kernel control block declarations.
4 */
5
6/*
7 * Copyright (c) 2013, 2014, ETH Zurich.
8 * All rights reserved.
9 *
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, Universitaetstr. 6, CH-8092 Zurich. Attn: Systems Group.
13 */
14
15#ifndef KCB_H
16#define KCB_H
17
18#include <kernel.h>
19#include <capabilities.h>
20#include <irq.h>
21#include <mdb/mdb_tree.h>
22
23struct cte;
24struct dcb;
25
26enum sched_state {
27    SCHED_RR,
28    SCHED_RBED,
29};
30
31/**
32 * this is the memory layout of ObjType_KernelControlBlock
33 * this struct should contain all the persistent state that belongs to a
34 * kernel.
35 */
36struct kcb {
37    bool is_valid; ///< kcb has been initialized by a kernel before
38
39    /// kcb scheduling ring.
40    /// These fields point to the next and previous kcb that should be
41    /// scheduled when we're running multiple kcbs on the same kernel.
42    /// invariant: next == NULL --> prev == NULL
43    /// invariant: next is the next kcb in the ring and prev is the previous
44    ///            kcb in the ring
45    struct kcb *next, *prev;
46
47    /// mdb root node
48    lvaddr_t mdb_root;
49    // XXX: need memory for a rootcn here because we can't have it static in
50    // the kernel data section anymore
51    struct cte init_rootcn;
52
53    /// which scheduler state is valid
54    enum sched_state sched;
55    /// RR scheduler state
56    struct dcb *ring_current;
57    /// RBED scheduler state
58    struct dcb *queue_head, *queue_tail;
59    unsigned int u_hrt, u_srt, w_be, n_be;
60    /// current time since kernel start in timeslices. This is necessary to
61    /// make the scheduler work correctly
62    /// wakeup queue head
63    struct dcb *wakeup_queue_head;
64    /// last value of kernel_now before shutdown/migration
65    //needs to be signed because it's possible to migrate a kcb onto a cpu
66    //driver whose kernel_now > this kcb's kernel_off.
67    int64_t kernel_off;
68
69    uint8_t irq_in_use[NDISPATCH / 8]; // Bitmap of handed out caps.
70    struct cte irq_dispatch[NDISPATCH];
71    // TODO: maybe add a shared part which can replace struct core_data?
72};
73
74///< The kernel control block
75extern struct kcb *kcb_current;
76///< flag that indicates whether kcb scheduling should happen
77extern bool kcb_sched_suspended;
78
79static inline void print_kcb(void)
80{
81    printk(LOG_DEBUG, "kcb contents:\n");
82    printk(LOG_DEBUG, "  next = %p, prev = %p\n",
83            kcb_current->next, kcb_current->prev);
84    printk(LOG_DEBUG, "  mdb_root = 0x%"PRIxLVADDR"\n", kcb_current->mdb_root);
85    printk(LOG_DEBUG, "  queue_head = %p\n", kcb_current->queue_head);
86    printk(LOG_DEBUG, "  queue_tail = %p\n", kcb_current->queue_tail);
87    printk(LOG_DEBUG, "  wakeup_queue_head = %p\n", kcb_current->wakeup_queue_head);
88    printk(LOG_DEBUG, "  u_hrt = %u, u_srt = %u, w_be = %u, n_be = %u\n",
89            kcb_current->u_hrt, kcb_current->u_srt, kcb_current->w_be,
90            kcb_current->n_be);
91    // TODO interrupt state
92}
93
94// XXX: this is from RBED, don't know how to properly have this here -SG
95extern struct dcb *queue_tail;
96static inline void switch_kcb(struct kcb *next)
97{
98    assert (next != NULL);
99    kcb_current = next;
100    mdb_init(kcb_current);
101    // update queue tail to make associated assembly not choke
102    queue_tail = kcb_current->queue_tail;
103}
104
105void kcb_add(struct kcb* new_kcb);
106errval_t kcb_remove(struct kcb *to_remove);
107void kcb_update_core_id(struct kcb *kcb);
108
109#endif
110