aboutsummaryrefslogtreecommitdiffstats
path: root/.github/pull_request_template
Commit message (Expand)AuthorAgeFilesLines
* build: Update README & github helpKevin Darbyshire-Bryant2018-07-081-1/+1
* merge: github: use OpenWrt in issue/pr templatesStijn Tintel2018-01-031-1/+1
* github: include pull request templateAlberto Bursi2017-03-121-0/+8
8 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
/******************************************************************************
 * domain.c
 * 
 * Generic domain-handling functions.
 */

#include <xen/config.h>
#include <xen/init.h>
#include <xen/lib.h>
#include <xen/errno.h>
#include <xen/sched.h>
#include <xen/mm.h>
#include <xen/event.h>
#include <xen/time.h>
#include <xen/console.h>
#include <asm/shadow.h>
#include <hypervisor-ifs/dom0_ops.h>
#include <asm/domain_page.h>

/* Both these structures are protected by the tasklist_lock. */
rwlock_t tasklist_lock __cacheline_aligned = RW_LOCK_UNLOCKED;
struct domain *task_hash[TASK_HASH_SIZE];
struct domain *task_list;

struct domain *do_createdomain(domid_t dom_id, unsigned int cpu)
{
    char buf[100];
    struct domain *d, **pd;
    unsigned long flags;

    if ( (d = alloc_domain_struct()) == NULL )
        return NULL;

    atomic_set(&d->refcnt, 1);
    atomic_set(&d->pausecnt, 0);

    shadow_lock_init(d);

    d->domain    = dom_id;
    d->processor = cpu;
    d->create_time = NOW();
    /* Initialise the sleep_lock */
    spin_lock_init(&d->sleep_lock);
 
    memcpy(&d->thread, &idle0_task.thread, sizeof(d->thread));

    if ( d->domain != IDLE_DOMAIN_ID )
    {
        if ( init_event_channels(d) != 0 )
        {
            free_domain_struct(d);
            return NULL;
        }
        
        /* We use a large intermediate to avoid overflow in sprintf. */
        sprintf(buf, "Domain-%u", dom_id);
        strncpy(d->name, buf, MAX_DOMAIN_NAME);
        d->name[MAX_DOMAIN_NAME-1] = '\0';

        d->addr_limit = USER_DS;
        
        spin_lock_init(&d->page_alloc_lock);
        INIT_LIST_HEAD(&d->page_list);
        d->max_pages = d->tot_pages = 0;

	arch_do_createdomain(d);

        /* Per-domain PCI-device list. */
        spin_lock_init(&d->pcidev_lock);
        INIT_LIST_HEAD(&d->pcidev_list);

        sched_add_domain(d);

        write_lock_irqsave(&tasklist_lock, flags);
        pd = &task_list; /* NB. task_list is maintained in order of dom_id. */
        for ( pd = &task_list; *pd != NULL; pd = &(*pd)->next_list )
            if ( (*pd)->domain > d->domain )
                break;
        d->next_list = *pd;
        *pd = d;
        d->next_hash = task_hash[TASK_HASH(dom_id)];
        task_hash[TASK_HASH(dom_id)] = d;
        write_unlock_irqrestore(&tasklist_lock, flags);
    }
    else
    {
        sprintf(d->name, "Idle-%d", cpu);
        sched_add_domain(d);
    }

    return d;
}


struct domain *find_domain_by_id(domid_t dom)
{
    struct domain *d;
    unsigned long flags;

    read_lock_irqsave(&tasklist_lock, flags);
    d = task_hash[TASK_HASH(dom)];
    while ( d != NULL )
    {
        if ( d->domain == dom )
        {
            if ( unlikely(!get_domain(d)) )
                d = NULL;
            break;
        }
        d = d->next_hash;
    }
    read_unlock_irqrestore(&tasklist_lock, flags);

    return d;
}


/* Return the most recently created domain. */
struct domain *find_last_domain(void)
{
    struct domain *d, *dlast;
    unsigned long flags;

    read_lock_irqsave(&tasklist_lock, flags);
    dlast = task_list;
    d = dlast->next_list;
    while ( d != NULL )
    {
        if ( d->create_time > dlast->create_time )
            dlast = d;
        d = d->next_list;
    }
    if ( !get_domain(dlast) )
        dlast = NULL;
    read_unlock_irqrestore(&tasklist_lock, flags);

    return dlast;
}


void domain_kill(struct domain *d)
{
    domain_pause(d);
    if ( !test_and_set_bit(DF_DYING, &d->flags) )
    {
        sched_rem_domain(d);
        domain_relinquish_memory(d);
        put_domain(d);
    }
}


void domain_crash(void)
{
    struct domain *d;

    set_bit(DF_CRASHED, &current->flags);

    d = find_domain_by_id(0);
    send_guest_virq(d, VIRQ_DOM_EXC);
    put_domain(d);
    
    __enter_scheduler();
    BUG();
}

void domain_shutdown(u8 reason)
{
    struct domain *d;

    if ( current->domain == 0 )
    {
        extern void machine_restart(char *);
        extern void machine_halt(void);

        if ( reason == 0 ) 
        {
            printk("Domain 0 halted: Our work here is done.\n");
            machine_halt();
        }
        else
        {
            printk("Domain 0 shutdown: rebooting machine!\n");
            machine_restart(0);
        }
    }

    current->shutdown_code = reason;
    set_bit(DF_SHUTDOWN, &current->flags);

    d = find_domain_by_id(0);
    send_guest_virq(d, VIRQ_DOM_EXC);
    put_domain(d);

    __enter_scheduler();
}

unsigned int alloc_new_dom_mem(struct domain *d, unsigned int kbytes)
{
    unsigned int alloc_pfns, nr_pages;
    struct pfn_info *page;

    nr_pages = (kbytes + ((PAGE_SIZE-1)>>10)) >> (PAGE_SHIFT - 10);
    d->max_pages = nr_pages; /* this can now be controlled independently */

    /* Grow the allocation if necessary. */
    for ( alloc_pfns = d->tot_pages; alloc_pfns < nr_pages; alloc_pfns++ )
    {
        if ( unlikely((page = alloc_domheap_page(d)) == NULL) )
        {
            domain_relinquish_memory(d);
            return -ENOMEM;
        }

        /* initialise to machine_to_phys_mapping table to likely pfn */
        machine_to_phys_mapping[page-frame_table] = alloc_pfns;

#ifndef NDEBUG
        {
            /* Initialise with magic marker if in DEBUG mode. */
            void *a = map_domain_mem((page-frame_table)<<PAGE_SHIFT);
            memset(a, 0x80 | (char)d->domain, PAGE_SIZE);
            unmap_domain_mem(a);
        }
#endif
    }

    return 0;
}
 

/* Release resources belonging to task @p. */
void domain_destruct(struct domain *d)
{
    struct domain **pd;
    unsigned long flags;

    if ( !test_bit(DF_DYING, &d->flags) )
        BUG();

    /* May be already destructed, or get_domain() can race us. */
    if ( cmpxchg(&d->refcnt.counter, 0, DOMAIN_DESTRUCTED) != 0 )
        return;

    DPRINTK("Releasing task %u\n", d->domain);

    /* Delete from task list and task hashtable. */
    write_lock_irqsave(&tasklist_lock, flags);
    pd = &task_list;
    while ( *pd != d ) 
        pd = &(*pd)->next_list;
    *pd = d->next_list;
    pd = &task_hash[TASK_HASH(d->domain)];
    while ( *pd != d ) 
        pd = &(*pd)->next_hash;
    *pd = d->next_hash;
    write_unlock_irqrestore(&tasklist_lock, flags);

    destroy_event_channels(d);

    free_perdomain_pt(d);
    free_xenheap_page((unsigned long)d->shared_info);

    free_domain_struct(d);
}


/*
 * final_setup_guestos is used for final setup and launching of domains other
 * than domain 0. ie. the domains that are being built by the userspace dom0
 * domain builder.
 */
int final_setup_guestos(struct domain *p, dom0_builddomain_t *builddomain)
{
    int rc = 0;
    full_execution_context_t *c;

    if ( (c = xmalloc(sizeof(*c))) == NULL )
        return -ENOMEM;

    if ( test_bit(DF_CONSTRUCTED, &p->flags) )
    {
        rc = -EINVAL;
        goto out;
    }

    if ( copy_from_user(c, builddomain->ctxt, sizeof(*c)) )
    {
        rc = -EFAULT;
        goto out;
    }
    
    arch_final_setup_guestos(p,c);

    /* Set up the shared info structure. */
    update_dom_time(p->shared_info);

    set_bit(DF_CONSTRUCTED, &p->flags);

 out:    
    if ( c != NULL )
        xfree(c);
    return rc;
}