forked from cherokee/webserver
/
cacheline.c
135 lines (106 loc) · 2.88 KB
/
cacheline.c
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2011 Alvaro Lopez Ortega
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "common-internal.h"
#include "cacheline.h"
#if (( __GNUC__ || __INTEL_COMPILER ) && ( __i386__ || __amd64__ ))
static void
get_cpuid (uint32_t i, uint32_t *buf)
{
# if __i386__
__asm__ (
" mov %%ebx, %%esi; "
" cpuid; "
" mov %%eax, (%1); "
" mov %%ebx, 4(%1); "
" mov %%edx, 8(%1); "
" mov %%ecx, 12(%1); "
" mov %%esi, %%ebx; "
: : "a" (i), "D" (buf) : "ecx", "edx", "esi", "memory" );
# elif __amd64__
uint32_t eax, ebx, ecx, edx;
__asm__ (
"cpuid"
: "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (i) );
buf[0] = eax;
buf[1] = ebx;
buf[2] = edx;
buf[3] = ecx;
# endif
}
static ret_t
get_cache_line_live (cuint_t *size)
{
uint32_t vendor_buf[5];
uint32_t processor_buf[5];
const char *vendor;
memset (vendor_buf, 0, sizeof(vendor_buf));
memset (processor_buf, 0, sizeof(processor_buf));
/* EAX=0: Get vendor ID
*/
get_cpuid (0, vendor_buf);
vendor = (const char *)&vendor_buf[1];
if (vendor_buf[0] == 0) {
return ret_error;
}
/* EAX=1: Get Processor Info and Feature Bits
*/
get_cpuid(1, processor_buf);
if (strcmp (vendor, "GenuineIntel") == 0) {
switch ((processor_buf[0] & 0xf00) >> 8) {
case 5:
/* Pentium */
*size = 32;
return ret_ok;
case 6:
/* Pentium Pro, II, III */
*size = 32;
if ((processor_buf[0] & 0xf0) >= 0xd0) {
/* Intel Core */
*size = 64;
}
return ret_ok;
case 15:
/* Pentium 4, although its cache line size is 64 bytes,
* it prefetches up to two cache lines during memory read
*/
*size = 128;
return ret_ok;
}
} else if (strcmp(vendor, "AuthenticAMD") == 0) {
*size = 64;
return ret_ok;
}
return ret_not_found;
}
#endif // (( __GNUC__ || __INTEL_COMPILER ) && ( __i386__ || __amd64__ ))
ret_t
cherokee_cacheline_size_get (cuint_t *size)
{
#if (( __GNUC__ || __INTEL_COMPILER ) && ( __i386__ || __amd64__ ))
ret_t ret;
ret = get_cache_line_live (size);
if (ret == ret_ok) return ret_ok;
#endif
*size = CPU_CACHE_LINE;
return ret_ok;
}