Parcourir la source

evx: add a time module

rain il y a 3 ans
Parent
commit
2ca52b1ad7

+ 2 - 1
Kconfig

@@ -1,4 +1,5 @@
 [	
 	{"CONFIG_64BIT": "Enable / disable 64-bits kernel support"}, 
-	{"CONFIG_KMLOG_LEVEL": "Messages smaller than this level will be ignored"}
+	{"CONFIG_KMLOG_LEVEL": "Messages smaller than this level will be ignored"}, 
+	{"CONFIG_TIMEZONE": "Your time zone (as UTC+/-X) e.g. +8, -2"}
 ]

+ 0 - 1
arch/x86/.archcflags

@@ -1 +0,0 @@
--m64 -I/root/evx/arch/x86/include -I/root/evx/arch/x86/include64 

+ 1 - 0
arch/x86/include/arch/time.h

@@ -0,0 +1 @@
+#include <modules/cmos.h>

+ 3 - 3
arch/x86/include/arch/types.h

@@ -1,4 +1,4 @@
-/* arch/x86/include/arch/string.h 
+/* arch/x86/include/arch/types.h 
  * 
  * Copyright (C) 2021, 2022 intirain. 
  *
@@ -21,8 +21,8 @@
 
 
 
-#ifndef __ARCH_STRING_H
-#define __ARCH_STRING_H
+#ifndef __ARCH_TYPES_H
+#define __ARCH_TYPES_H
 
 
 #ifndef __ASSEMBLER__

+ 0 - 4
arch/x86/kernel/arch_int.c

@@ -158,7 +158,6 @@ __asm__ (
 		"jmp ret_intr\n\t"
 );
 
-
 #define build_trap_entry_errcode(nr) 		\
 	void trap_##nr##_entry(void); 		\
 	__asm__ (				\
@@ -207,9 +206,6 @@ build_trap_entry_nerrcode(18);
 build_trap_entry_nerrcode(19);
 build_trap_entry_nerrcode(20);
 
-
-
-
 /* set the handlers for traps, 0 - 31 */
 
 int __init arch_traps_init(void) {

+ 3 - 0
include/config.h

@@ -0,0 +1,3 @@
+#define CONFIG_64BIT 			(1)
+#define CONFIG_KMLOG_LEVEL 			(-1)
+#define CONFIG_TIMEZONE 			(8)

+ 44 - 0
include/kernel/time.h

@@ -0,0 +1,44 @@
+/* include/kernel/time.h 
+ * 
+ * Copyright (C) 2021, 2022 intirain. 
+ *
+ * This file is part of evx. 
+ * 
+ * evx 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. 
+ * 
+ * evx 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 evx. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#ifndef __KTIME_H
+#define __KTIME_H
+
+#include <sys/types.h>
+
+struct ktime {
+	int year, month, day;
+	int hour, min, sec;
+};
+
+/* I've no interest in time zones. Only do the necessary things. */
+struct timezone {
+	/* UTC+X */
+	int utcplus;
+};
+
+extern struct ktime kernel_startup_time;
+void show_time(struct ktime *);
+void show_time_tz(struct ktime *, struct timezone *);
+
+#endif

+ 57 - 0
include/modules/cmos.h

@@ -0,0 +1,57 @@
+/* include/modules/cmos.h 
+ * 
+ * Copyright (C) 2021, 2022 intirain. 
+ *  
+ * This file is part of evx. 
+ * 
+ * evx 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. 
+ * 
+ * evx 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 evx. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#ifndef __MOD_CMOS_H
+#define __MOD_CMOS_H
+
+#ifndef __ASSEMBLER__
+
+#include <arch/io.h>
+#include <kernel/time.h>
+
+#define CMOS_READ(addr) ({	\
+	outb(addr, 0x70);	\
+	inb(0x71); })
+
+/* For time and they actually come from linux.  */
+#define BCD2BIN(byte) ((byte) = ((byte) & 0xf) + ((byte) >> 4) * 10)
+static inline void read_hw_clock(struct ktime *tm) {
+	do {
+		tm->sec = CMOS_READ(0);
+		tm->min = CMOS_READ(2);
+		tm->hour = CMOS_READ(4);
+		tm->day = CMOS_READ(7);
+		tm->month = CMOS_READ(8) - 1;
+		tm->year = CMOS_READ(9);
+	} while (tm->sec != CMOS_READ(0));
+
+	BCD2BIN(tm->sec); BCD2BIN(tm->min); BCD2BIN(tm->hour);
+	BCD2BIN(tm->day); BCD2BIN(tm->month); BCD2BIN(tm->year);
+
+	tm->year += tm->year >= 70 ? 1900 : 2000;
+	tm->month++;
+}
+
+#endif
+#endif
+

+ 1 - 1
kernel/Makefile

@@ -1,7 +1,7 @@
 # this was called from the Makefile in /, 
 # and we just tell it what objects that he should make. 
 
-obj-i = mm.c vsprintf.c tty.c mempool.c panic.c page.c kmlog.c task.c 
+obj-i = mm.c vsprintf.c tty.c mempool.c panic.c page.c kmlog.c task.c time.c
 
 target: $(obj-i)
 

+ 3 - 3
kernel/task.c

@@ -45,10 +45,10 @@ struct task *current = &init_task;
 int next_pid = 1;
 
 int task_init(void) {
-	struct task *new = dup_task();
+//	struct task *new = dup_task();
 
-	kmlog("New task: %p\n", new);
-	kmlog("task initialization finished. \n");
+//	kmlog("New task: %p\n", new);
+//	kmlog("task initialization finished. \n");
 
 //	switch_to(new);
 

+ 67 - 0
kernel/time.c

@@ -0,0 +1,67 @@
+/* kernel/time.c 
+ * 
+ * Copyright (C) 2021, 2022 intirain. 
+ *
+ * This file is part of evx. 
+ * 
+ * evx 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. 
+ * 
+ * evx 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 evx. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#include <kernel/init.h>
+#include <kernel/module.h>
+#include <kernel/time.h>
+#include <kernel/kmlog.h>
+#include <arch/time.h>
+#include <config.h>
+
+#include <sys/types.h>
+
+struct ktime kernel_startup_time;
+struct timezone timezone;
+
+int __init time_init(void) {
+	timezone.utcplus = CONFIG_TIMEZONE;
+	read_hw_clock(&kernel_startup_time);
+
+	show_time(&kernel_startup_time);
+	return 0;
+}
+
+void show_time(struct ktime *tm) {
+	show_time_tz(tm, &timezone);
+}
+
+void show_time_tz(struct ktime *tts, struct timezone *tz) {
+	struct ktime tm = *tts;
+
+	tm.hour += tz->utcplus;
+	if (tm.hour >= 24) {
+		tm.hour -= 24;
+		tm.day++;
+
+		/* I said, I've no interest in doing these 
+		 * BORING but necessary things. Just leave them here. */
+	}
+
+	kmlog(KERN_INFO "%d-%d-%d %d:%d:%d (UTC %c%d)\n", 
+			tm.year, tm.month, tm.day, 
+			tm.hour, tm.min, tm.sec,
+			tz->utcplus > 0 ? '+' : '-', tz->utcplus);
+}
+
+kernel_init(time_init);
+