summaryrefslogtreecommitdiff
path: root/kern/percpu.c
blob: 5b9690cc45bfb176f4654f651a2b106ca2d0c20a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
/*
 * Copyright (c) 2014 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <kern/assert.h>
#include <kern/error.h>
#include <kern/init.h>
#include <kern/macros.h>
#include <kern/panic.h>
#include <kern/param.h>
#include <kern/percpu.h>
#include <kern/printk.h>
#include <kern/stddef.h>
#include <kern/string.h>
#include <machine/cpu.h>
#include <vm/vm_kmem.h>
#include <vm/vm_page.h>

void *percpu_areas[MAX_CPUS] __read_mostly;

static void *percpu_area_content __initdata;
static size_t percpu_area_size __initdata;
static int percpu_skip_warning __initdata;

void __init
percpu_bootstrap(void)
{
    percpu_areas[0] = &_percpu;
}

void __init
percpu_setup(void)
{
    struct vm_page *page;
    unsigned int order;

    percpu_area_size = &_epercpu - &_percpu;
    printk("percpu: max_cpus: %u, section size: %zuk\n", MAX_CPUS,
           percpu_area_size >> 10);
    assert(vm_page_aligned(percpu_area_size));

    if (percpu_area_size == 0) {
        return;
    }

    order = vm_page_order(percpu_area_size);
    page = vm_page_alloc(order, VM_PAGE_SEL_DIRECTMAP, VM_PAGE_KERNEL);

    if (page == NULL) {
        panic("percpu: unable to allocate memory for percpu area content");
    }

    percpu_area_content = vm_page_direct_ptr(page);
    memcpy(percpu_area_content, &_percpu, percpu_area_size);
}

int __init
percpu_add(unsigned int cpu)
{
    struct vm_page *page;
    unsigned int order;

    if (cpu >= ARRAY_SIZE(percpu_areas)) {
        if (!percpu_skip_warning) {
            printk("percpu: ignoring processor beyond id %zu\n",
                   ARRAY_SIZE(percpu_areas) - 1);
            percpu_skip_warning = 1;
        }

        return ERROR_INVAL;
    }

    if (percpu_areas[cpu] != NULL) {
        printk("percpu: error: id %u ignored, already registered\n", cpu);
        return ERROR_INVAL;
    }

    if (percpu_area_size == 0) {
        goto out;
    }

    order = vm_page_order(percpu_area_size);
    page = vm_page_alloc(order, VM_PAGE_SEL_DIRECTMAP, VM_PAGE_KERNEL);

    if (page == NULL) {
        printk("percpu: error: unable to allocate percpu area\n");
        return ERROR_NOMEM;
    }

    percpu_areas[cpu] = vm_page_direct_ptr(page);
    memcpy(percpu_area(cpu), percpu_area_content, percpu_area_size);

out:
    return 0;
}

void
percpu_cleanup(void)
{
    struct vm_page *page;
    unsigned long va;

    va = (unsigned long)percpu_area_content;
    page = vm_page_lookup(vm_page_direct_pa(va));
    vm_page_free(page, vm_page_order(percpu_area_size));
}