blob: 72053192ffac2ad445336a62f2e95f009e13d4e2 [file] [log] [blame]
Igor Sysoevffe71442006-02-08 15:33:12 +00001
2/*
3 * Copyright (C) Igor Sysoev
Maxim Konovalovf8d59e32012-01-18 15:07:43 +00004 * Copyright (C) Nginx, Inc.
Igor Sysoevffe71442006-02-08 15:33:12 +00005 */
6
7
8#include <ngx_config.h>
9#include <ngx_core.h>
10
11
12#if (( __i386__ || __amd64__ ) && ( __GNUC__ || __INTEL_COMPILER ))
13
14
15static ngx_inline void ngx_cpuid(uint32_t i, uint32_t *buf);
16
17
Igor Sysoevcce886c2006-02-22 19:41:39 +000018#if ( __i386__ )
19
20static ngx_inline void
21ngx_cpuid(uint32_t i, uint32_t *buf)
22{
23
24 /*
25 * we could not use %ebx as output parameter if gcc builds PIC,
26 * and we could not save %ebx on stack, because %esp is used,
27 * when the -fomit-frame-pointer optimization is specified.
28 */
29
30 __asm__ (
31
32 " mov %%ebx, %%esi; "
33
34 " cpuid; "
Igor Sysoev6f134cc2006-05-23 14:54:58 +000035 " mov %%eax, (%1); "
36 " mov %%ebx, 4(%1); "
37 " mov %%edx, 8(%1); "
38 " mov %%ecx, 12(%1); "
Igor Sysoevcce886c2006-02-22 19:41:39 +000039
40 " mov %%esi, %%ebx; "
41
Igor Sysoev6f134cc2006-05-23 14:54:58 +000042 : : "a" (i), "D" (buf) : "ecx", "edx", "esi", "memory" );
Igor Sysoevcce886c2006-02-22 19:41:39 +000043}
44
45
46#else /* __amd64__ */
47
48
Igor Sysoevffe71442006-02-08 15:33:12 +000049static ngx_inline void
50ngx_cpuid(uint32_t i, uint32_t *buf)
51{
52 uint32_t eax, ebx, ecx, edx;
53
54 __asm__ (
55
56 "cpuid"
57
58 : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (i) );
59
60 buf[0] = eax;
61 buf[1] = ebx;
62 buf[2] = edx;
63 buf[3] = ecx;
64}
65
66
Igor Sysoevcce886c2006-02-22 19:41:39 +000067#endif
68
69
Igor Sysoevffe71442006-02-08 15:33:12 +000070/* auto detect the L2 cache line size of modern and widespread CPUs */
71
72void
73ngx_cpuinfo(void)
74{
75 u_char *vendor;
Igor Sysoev7c6c1862009-03-28 12:43:41 +000076 uint32_t vbuf[5], cpu[4], model;
Igor Sysoevffe71442006-02-08 15:33:12 +000077
78 vbuf[0] = 0;
79 vbuf[1] = 0;
80 vbuf[2] = 0;
81 vbuf[3] = 0;
82 vbuf[4] = 0;
83
84 ngx_cpuid(0, vbuf);
85
86 vendor = (u_char *) &vbuf[1];
87
88 if (vbuf[0] == 0) {
89 return;
90 }
91
92 ngx_cpuid(1, cpu);
93
94 if (ngx_strcmp(vendor, "GenuineIntel") == 0) {
95
Igor Sysoev442d1e62008-01-29 06:58:47 +000096 switch ((cpu[0] & 0xf00) >> 8) {
Igor Sysoevffe71442006-02-08 15:33:12 +000097
98 /* Pentium */
99 case 5:
Igor Sysoev48b7b9b2008-01-29 07:06:18 +0000100 ngx_cacheline_size = 32;
101 break;
102
Igor Sysoevffe71442006-02-08 15:33:12 +0000103 /* Pentium Pro, II, III */
104 case 6:
105 ngx_cacheline_size = 32;
Igor Sysoev48b7b9b2008-01-29 07:06:18 +0000106
Igor Sysoev7c6c1862009-03-28 12:43:41 +0000107 model = ((cpu[0] & 0xf0000) >> 8) | (cpu[0] & 0xf0);
108
109 if (model >= 0xd0) {
110 /* Intel Core, Core 2, Atom */
Igor Sysoev48b7b9b2008-01-29 07:06:18 +0000111 ngx_cacheline_size = 64;
112 }
113
Igor Sysoevffe71442006-02-08 15:33:12 +0000114 break;
115
116 /*
117 * Pentium 4, although its cache line size is 64 bytes,
118 * it prefetches up to two cache lines during memory read
119 */
120 case 15:
121 ngx_cacheline_size = 128;
122 break;
123 }
124
125 } else if (ngx_strcmp(vendor, "AuthenticAMD") == 0) {
126 ngx_cacheline_size = 64;
127 }
128}
129
130#else
131
132
133void
134ngx_cpuinfo(void)
135{
136}
137
138
139#endif