summaryrefslogtreecommitdiff
path: root/util/cache-utils.c
blob: 047003008bc3b7e08d964075fcb2095d6958a09e (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
#include "qemu-common.h"
#include "qemu/cache-utils.h"

#if defined(_ARCH_PPC)
struct qemu_cache_conf qemu_cache_conf = {
    .dcache_bsize = 16,
    .icache_bsize = 16
};

#if defined _AIX
#include <sys/systemcfg.h>

void qemu_cache_utils_init(void)
{
    qemu_cache_conf.icache_bsize = _system_configuration.icache_line;
    qemu_cache_conf.dcache_bsize = _system_configuration.dcache_line;
}

#elif defined __linux__
#include "qemu/osdep.h"
#include "elf.h"

void qemu_cache_utils_init(void)
{
    unsigned long dsize = qemu_getauxval(AT_DCACHEBSIZE);
    unsigned long isize = qemu_getauxval(AT_ICACHEBSIZE);

    if (dsize == 0 || isize == 0) {
        if (dsize == 0) {
            fprintf(stderr, "getauxval AT_DCACHEBSIZE failed\n");
        }
        if (isize == 0) {
            fprintf(stderr, "getauxval AT_ICACHEBSIZE failed\n");
        }
        exit(1);

    }
    qemu_cache_conf.dcache_bsize = dsize;
    qemu_cache_conf.icache_bsize = isize;
}

#elif defined __APPLE__
#include <stdio.h>
#include <sys/types.h>
#include <sys/sysctl.h>

void qemu_cache_utils_init(void)
{
    size_t len;
    unsigned cacheline;
    int name[2] = { CTL_HW, HW_CACHELINE };

    len = sizeof(cacheline);
    if (sysctl(name, 2, &cacheline, &len, NULL, 0)) {
        perror("sysctl CTL_HW HW_CACHELINE failed");
    } else {
        qemu_cache_conf.dcache_bsize = cacheline;
        qemu_cache_conf.icache_bsize = cacheline;
    }
}

#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/sysctl.h>

void qemu_cache_utils_init(void)
{
    size_t len = 4;
    unsigned cacheline;

    if (sysctlbyname ("machdep.cacheline_size", &cacheline, &len, NULL, 0)) {
        fprintf(stderr, "sysctlbyname machdep.cacheline_size failed: %s\n",
                strerror(errno));
        exit(1);
    }

    qemu_cache_conf.dcache_bsize = cacheline;
    qemu_cache_conf.icache_bsize = cacheline;
}
#endif

#endif /* _ARCH_PPC */