New developer version 0.6.8; added select () function; added demonstrating example...
[ZeXOS.git] / kernel / core / smp.c
blobaf8364fff7b41c6fa9787e677f09efdcf1bb3dd9
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <system.h>
21 #include <string.h>
22 #include <config.h>
23 #include <smp.h>
24 #include <vfs.h>
26 smp_cpu_t smp_cpu_list;
28 smp_cpu_t *smp_cpu_getnextcpu (smp_cpu_t *cpu)
30 if (!cpu)
31 return smp_cpu_list.next;
33 smp_cpu_t *nextcpu = cpu->next;
35 if (cpu == &smp_cpu_list)
36 return 0;
38 return nextcpu;
41 void smp_cpu_check ()
43 unsigned cpu_cnt = 1;
45 char *str = (char *) kmalloc (sizeof (char) * (cpu_cnt * 256));
47 if (!str)
48 return;
50 char strcpu[80];
52 char cpuarch[16];
54 smp_cpu_t *cpu;
55 for (cpu = smp_cpu_list.next; cpu != &smp_cpu_list; cpu = cpu->next) {
56 if (cpu->arch == SMP_ARCH_x86)
57 strcpy (cpuarch, "x86");
58 else if (cpu->arch == SMP_ARCH_ARM)
59 strcpy (cpuarch, "ARM");
60 else if (cpu->arch != SMP_ARCH_x86)
61 strcpy (cpuarch, "unknown");
63 sprintf (strcpu, "CPU %d (%s)\n\tarch: %s\n\tstate: %s\n\tfeatures: ", cpu_cnt, (cpu->flags == SMP_FLAGS_CPU_BS ) ? "BS" : "AP",
64 cpuarch, (cpu->state == SMP_STATE_CPU_UP ) ? "running" : "down");
66 strcat (str, strcpu);
68 smp_arch_x86 *arch_spec = cpu->arch_spec;
70 if (!arch_spec) {
71 strcat (str, "N/A");
72 goto r;
75 if (arch_spec->features & SMP_ARCH_x86_FEATURE_FPU)
76 strcat (str, "fpu ");
77 if (arch_spec->features & SMP_ARCH_x86_FEATURE_VME)
78 strcat (str, "vme ");
79 if (arch_spec->features & SMP_ARCH_x86_FEATURE_DE)
80 strcat (str, "de ");
81 if (arch_spec->features & SMP_ARCH_x86_FEATURE_PSE)
82 strcat (str, "pse ");
83 if (arch_spec->features & SMP_ARCH_x86_FEATURE_TSC)
84 strcat (str, "tsc ");
85 if (arch_spec->features & SMP_ARCH_x86_FEATURE_MSR)
86 strcat (str, "msr ");
87 if (arch_spec->features & SMP_ARCH_x86_FEATURE_PAE)
88 strcat (str, "pae ");
89 if (arch_spec->features & SMP_ARCH_x86_FEATURE_MCE)
90 strcat (str, "mce ");
91 if (arch_spec->features & SMP_ARCH_x86_FEATURE_CX8)
92 strcat (str, "cx8 ");
93 if (arch_spec->features & SMP_ARCH_x86_FEATURE_APIC)
94 strcat (str, "apic ");
95 if (arch_spec->features & SMP_ARCH_x86_FEATURE_SEP)
96 strcat (str, "sep ");
97 if (arch_spec->features & SMP_ARCH_x86_FEATURE_MTRR)
98 strcat (str, "mttr ");
99 if (arch_spec->features & SMP_ARCH_x86_FEATURE_PGE)
100 strcat (str, "pge ");
101 if (arch_spec->features & SMP_ARCH_x86_FEATURE_MCA)
102 strcat (str, "mca ");
103 if (arch_spec->features & SMP_ARCH_x86_FEATURE_CMOV)
104 strcat (str, "cmov ");
105 if (arch_spec->features & SMP_ARCH_x86_FEATURE_PAT)
106 strcat (str, "pat ");
107 if (arch_spec->features & SMP_ARCH_x86_FEATURE_PSE36)
108 strcat (str, "pse36 ");
109 if (arch_spec->features & SMP_ARCH_x86_FEATURE_PSN)
110 strcat (str, "psn ");
111 if (arch_spec->features & SMP_ARCH_x86_FEATURE_CLFSH)
112 strcat (str, "clfsh ");
113 if (arch_spec->features & SMP_ARCH_x86_FEATURE_DS)
114 strcat (str, "ds ");
115 if (arch_spec->features & SMP_ARCH_x86_FEATURE_ACPI)
116 strcat (str, "acpi ");
117 if (arch_spec->features & SMP_ARCH_x86_FEATURE_MMX)
118 strcat (str, "mmx ");
119 if (arch_spec->features & SMP_ARCH_x86_FEATURE_FXSR)
120 strcat (str, "fxsr ");
121 if (arch_spec->features & SMP_ARCH_x86_FEATURE_SSE)
122 strcat (str, "sse ");
123 if (arch_spec->features & SMP_ARCH_x86_FEATURE_SSE2)
124 strcat (str, "sse2 ");
125 if (arch_spec->features & SMP_ARCH_x86_FEATURE_SS)
126 strcat (str, "ss ");
127 if (arch_spec->features & SMP_ARCH_x86_FEATURE_HTT)
128 strcat (str, "htt ");
129 if (arch_spec->features & SMP_ARCH_x86_FEATURE_TM)
130 strcat (str, "tm ");
131 if (arch_spec->features & SMP_ARCH_x86_FEATURE_PBE)
132 strcat (str, "pbe ");
134 strcat (str, "\n");
136 cpu_cnt ++;
139 vfs_list_add ("cpuinfo", VFS_FILEATTR_FILE | VFS_FILEATTR_READ | VFS_FILEATTR_SYSTEM, "/proc/");
141 vfs_content_t content;
142 content.ptr = str;
143 content.len = strlen (str);
145 vfs_mmap ("/proc/cpuinfo", 13, &content);
148 /* mostly x86 stuff */
149 unsigned smp_cpu_register (unsigned char arch, unsigned char state, unsigned char flags, void *arch_spec)
151 smp_cpu_t *cpu;
153 /* alloc and init context */
154 cpu = (smp_cpu_t *) kmalloc (sizeof (smp_cpu_t));
156 if (!cpu)
157 return 0;
159 cpu->arch = arch;
160 cpu->arch_spec = arch_spec;
161 cpu->flags = flags;
162 cpu->state = state;
164 /* add into list */
165 cpu->next = &smp_cpu_list;
166 cpu->prev = smp_cpu_list.prev;
167 cpu->prev->next = cpu;
168 cpu->next->prev = cpu;
170 return 1;
173 unsigned int init_smp ()
175 smp_cpu_list.next = &smp_cpu_list;
176 smp_cpu_list.prev = &smp_cpu_list;
178 return arch_smp_init ();