Improve formatting and enable FLOATING_POINT_ERR

This commit is contained in:
Enrico Fraccaroli (Galfurian)
2023-08-11 13:39:45 -04:00
parent 20091b196f
commit 8578f078c6
2 changed files with 35 additions and 21 deletions
+18 -14
View File
@@ -9,13 +9,13 @@
#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level.
#include "io/debug.h" // Include debugging functions.
#include "devices/fpu.h"
#include "descriptor_tables/isr.h"
#include "string.h"
#include "assert.h"
#include "process/scheduler.h"
#include "descriptor_tables/isr.h"
#include "devices/fpu.h"
#include "math.h"
#include "process/process.h"
#include "process/scheduler.h"
#include "string.h"
#include "system/signal.h"
/// Pointerst to the current thread using the FPU.
@@ -36,12 +36,12 @@ static inline void __enable_fpu()
__asm__ __volatile__("clts");
size_t t;
__asm__ __volatile__("mov %%cr0, %0"
: "=r"(t));
: "=r"(t));
t &= ~(1U << 2U);
t |= (1U << 1U);
__asm__ __volatile__("mov %0, %%cr0" ::"r"(t));
__asm__ __volatile__("mov %%cr4, %0"
: "=r"(t));
: "=r"(t));
t |= 3U << 9U;
__asm__ __volatile__("mov %0, %%cr4" ::"r"(t));
}
@@ -52,7 +52,7 @@ static inline void __disable_fpu()
size_t t;
__asm__ __volatile__("mov %%cr0, %0"
: "=r"(t));
: "=r"(t));
t |= 1U << 3U;
@@ -116,7 +116,7 @@ static inline void __invalid_op(pt_regs *f)
/// Kernel trap for various integer and floating-point errors
/// @param f The interrupt stack frame.
static inline void __sigfpe_handler(pt_regs* f)
static inline void __sigfpe_handler(pt_regs *f)
{
pr_debug("__sigfpe_handler(%p)\n", f);
@@ -125,7 +125,6 @@ static inline void __sigfpe_handler(pt_regs* f)
sys_kill(thread_using_fpu->pid, SIGFPE);
}
/// @brief Ensure basic FPU functionality works.
/// @details
/// For processors without a FPU, this tests that maths libraries link
@@ -137,17 +136,21 @@ static int __fpu_test()
for (int i = 0; i < 10000; i++) {
a = a * 1.123 + (a / 3);
a /= 1.111;
while (a > 100.0)
while (a > 100.0) {
a /= 3.1234563212;
while (a < 2.0)
}
while (a < 2.0) {
a += 1.1232132131;
}
}
if (a != 50.11095685350556294679336133413)
if (a != 50.11095685350556294679336133413) {
return 0;
}
// Second test.
a = M_PI;
for (int i = 0; i < 100; i++)
for (int i = 0; i < 100; i++) {
a = a * 3 + (a / 3);
}
return (a == 60957114488184560000000000000000000000000000000000000.0);
}
@@ -173,9 +176,10 @@ int fpu_install()
// Install handlers for floating points and integers errors
isr_install_handler(DIVIDE_ERROR, &__sigfpe_handler, "divide error");
isr_install_handler(FLOATING_POINT_ERR, &__sigfpe_handler, "floating point error");
// NB: The exceptions bolow don't seems to ever trigger
//isr_install_handler(OVERFLOW, &__sigfpe_handler, "overflow");
//isr_install_handler(FLOATING_POINT_ERR, &__sigfpe_handler, "floating point error");
return __fpu_test();
}
+17 -7
View File
@@ -12,21 +12,31 @@
#include <sys/wait.h>
#include <time.h>
void sig_handler(int sig)
{
printf("handler(%d) : Starting handler.\n", sig);
if (sig == SIGFPE) {
printf("handler(%d) : Correct signal. FPE\n", sig);
printf("handler(%d) : Exiting\n", sig);
exit(1);
// exit(1);
} else {
printf("handler(%d) : Wrong signal.\n", sig);
}
printf("handler(%d) : Ending handler.\n", sig);
}
long int fact(int n)
{
if (n == 0 || n == 1) {
return 1;
}
long int pro = 1;
while (n != 1) {
pro *= n--;
}
return pro;
}
int main(int argc, char *argv[])
{
sigaction_t action;
@@ -42,9 +52,9 @@ int main(int argc, char *argv[])
// Should trigger ALU error, fighting the compiler...
int d = 1, e = 1;
while (1) {
d /= e;
e -= 1;
}
d /= e;
e -= 1;
d /= e;
e -= 1;
printf("d: %d, e: %d\n", d, e);
}