Wrap single line statements inside braces.

This commit is contained in:
Enrico Fraccaroli (Galfurian)
2023-08-24 12:17:16 -04:00
parent f35abd187b
commit d50d997d6e
110 changed files with 1252 additions and 930 deletions
+1 -1
View File
@@ -246,4 +246,4 @@ ssize_t getdents(int fd, dirent_t *dirp, unsigned int count);
/// shall return a non-zero value that is the number of seconds until the
/// previous request would have generated a SIGALRM signal. Otherwise, alarm()
/// shall return 0.
int alarm(int seconds);
unsigned alarm(int seconds);
+1 -2
View File
@@ -4,9 +4,9 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "string.h"
#include "signal.h"
#include "stdio.h"
#include "string.h"
/// @brief Since there could be signal handlers listening for the abort, we need
/// to keep track at which stage of the abort we are.
@@ -28,7 +28,6 @@ void abort(void)
/* Send signal which possibly calls a user handler. */
if (stage == 1) {
// We must allow recursive calls of abort
int save_stage = stage;
+1 -1
View File
@@ -4,8 +4,8 @@
/// See LICENSE.md for details.
#include "assert.h"
#include "stdlib.h"
#include "stdio.h"
#include "stdlib.h"
void __assert_fail(const char *assertion, const char *file, const char *function, unsigned int line)
{
+2 -1
View File
@@ -83,8 +83,9 @@ static void cvt(double arg, int ndigits, int *decpt, int *sign, char *buf, unsig
*p1 = '1';
(*decpt)++;
if (eflag == 0) {
if (p > buf)
if (p > buf) {
*p = '0';
}
p++;
}
}
+29 -17
View File
@@ -4,13 +4,13 @@
/// See LICENSE.md for details.
#include "grp.h"
#include "sys/unistd.h"
#include "sys/errno.h"
#include "io/debug.h"
#include "assert.h"
#include "string.h"
#include "stdio.h"
#include "fcntl.h"
#include "io/debug.h"
#include "stdio.h"
#include "string.h"
#include "sys/errno.h"
#include "sys/unistd.h"
/// Holds the file descriptor while we are working with `/etc/group`.
static int __fd = -1;
@@ -24,14 +24,17 @@ static inline void __parse_line(group_t *grp, char *buf)
assert(grp && "Received null grp!");
char *token;
// Parse the group name.
if ((token = strtok(buf, ":")) != NULL)
if ((token = strtok(buf, ":")) != NULL) {
grp->gr_name = token;
}
// Parse the group passwd.
if ((token = strtok(NULL, ":")) != NULL)
if ((token = strtok(NULL, ":")) != NULL) {
grp->gr_passwd = token;
}
// Parse the group id.
if ((token = strtok(NULL, ":")) != NULL)
if ((token = strtok(NULL, ":")) != NULL) {
grp->gr_gid = atoi(token);
}
size_t found_users = 0;
while ((token = strtok(NULL, ",\n\0")) != NULL && found_users < MAX_MEMBERS_PER_GROUP) {
@@ -57,8 +60,9 @@ static inline char *__search_entry(int fd, char *buf, int buflen, const char *na
int pos = 0;
while ((ret = read(fd, &c, 1U))) {
// Skip carriage return.
if (c == '\r')
if (c == '\r') {
continue;
}
if (pos >= buflen) {
errno = ERANGE;
return NULL;
@@ -69,8 +73,9 @@ static inline char *__search_entry(int fd, char *buf, int buflen, const char *na
buf[pos] = 0;
// Check the entry.
if (name) {
if (strncmp(buf, name, strlen(name)) == 0)
if (strncmp(buf, name, strlen(name)) == 0) {
return buf;
}
} else {
int gid_start = -1, col_count = 0;
for (int i = 0; i < pos; ++i) {
@@ -85,15 +90,17 @@ static inline char *__search_entry(int fd, char *buf, int buflen, const char *na
// Parse the gid.
int found_gid = atoi(&buf[gid_start]);
// Check the gid.
if (found_gid == gid)
if (found_gid == gid) {
return buf;
}
}
}
// Reset the index.
pos = 0;
// If we have reached the EOF stop.
if (ret == EOF)
if (ret == EOF) {
break;
}
} else {
buf[pos++] = c;
}
@@ -108,23 +115,26 @@ group_t *getgrgid(gid_t gid)
static char buffer[BUFSIZ];
group_t *result;
if (!getgrgid_r(gid, &grp, buffer, BUFSIZ, &result))
if (!getgrgid_r(gid, &grp, buffer, BUFSIZ, &result)) {
return NULL;
}
return &grp;
}
group_t *getgrnam(const char *name)
{
if (name == NULL)
if (name == NULL) {
return NULL;
}
static group_t grp;
static char buffer[BUFSIZ];
group_t *result;
if (!getgrnam_r(name, &grp, buffer, BUFSIZ, &result))
if (!getgrnam_r(name, &grp, buffer, BUFSIZ, &result)) {
return NULL;
}
return &grp;
}
@@ -199,8 +209,9 @@ group_t *getgrent(void)
static char buffer[BUFSIZ];
while ((ret = read(__fd, &c, 1U))) {
// Skip carriage return.
if (c == '\r')
if (c == '\r') {
continue;
}
if (pos >= BUFSIZ) {
errno = ERANGE;
@@ -220,8 +231,9 @@ group_t *getgrent(void)
}
// If we have reached the EOF stop.
if (ret == EOF)
if (ret == EOF) {
break;
}
} else {
buffer[pos++] = c;
+12 -8
View File
@@ -4,12 +4,12 @@
/// See LICENSE.md for details.
#include "io/debug.h"
#include "io/port_io.h"
#include "io/ansi_colors.h"
#include "sys/bitops.h"
#include "string.h"
#include "stdio.h"
#include "io/port_io.h"
#include "math.h"
#include "stdio.h"
#include "string.h"
#include "sys/bitops.h"
/// Serial port for QEMU.
#define SERIAL_COM1 (0x03F8)
@@ -23,8 +23,9 @@ void dbg_putchar(char c)
void dbg_puts(const char *s)
{
while ((*s) != 0)
while ((*s) != 0) {
dbg_putchar(*s++);
}
}
static inline void __debug_print_header(const char *file, const char *fun, int line, short log_level, char *header)
@@ -76,8 +77,9 @@ static inline void __debug_print_header(const char *file, const char *fun, int l
void set_log_level(int level)
{
if ((level >= LOGLEVEL_EMERG) && (level <= LOGLEVEL_DEBUG))
if ((level >= LOGLEVEL_EMERG) && (level <= LOGLEVEL_DEBUG)) {
max_log_level = level;
}
}
int get_log_level()
@@ -92,8 +94,9 @@ void dbg_printf(const char *file, const char *fun, int line, char *header, short
static short new_line = 1;
// Stage 1: FORMAT
if (strlen(format) >= BUFSIZ)
if (strlen(format) >= BUFSIZ) {
return;
}
// Start variabile argument's list.
va_list ap;
@@ -132,8 +135,9 @@ const char *to_human_size(unsigned long bytes)
int i = 0;
double dblBytes = bytes;
if (bytes > 1024) {
for (i = 0; (bytes / 1024) > 0 && i < length - 1; i++, bytes /= 1024)
for (i = 0; (bytes / 1024) > 0 && i < length - 1; i++, bytes /= 1024) {
dblBytes = bytes / 1024.0;
}
}
sprintf(output, "%.02lf %2s", dblBytes, suffix[i]);
return output;
+2 -2
View File
@@ -4,11 +4,11 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "assert.h"
#include "stdlib.h"
#include "string.h"
#include "assert.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
/// @brief Reference to the `environ` variable in `setenv.c`.
extern char **environ;
+4 -4
View File
@@ -3,13 +3,13 @@
/// @copyright (c) 2014-2023 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "libgen.h"
#include "io/debug.h"
#include "limits.h"
#include "string.h"
#include <assert.h>
#include <stdlib.h>
#include <sys/unistd.h>
#include "libgen.h"
#include "string.h"
#include "limits.h"
#include "io/debug.h"
int dirname(const char *path, char *buffer, size_t buflen)
{
+10 -5
View File
@@ -18,26 +18,30 @@ double round(double x)
double floor(double x)
{
if (x > -1.0 && x < 1.0) {
if (x >= 0)
if (x >= 0) {
return 0.0;
}
return -1.0;
}
int i = (int)x;
if (x < 0)
if (x < 0) {
return (double)(i - 1);
}
return (double)i;
}
double ceil(double x)
{
if (x > -1.0 && x < 1.0) {
if (x <= 0)
if (x <= 0) {
return 0.0;
}
return 1.0;
}
int i = (int)x;
if (x > 0)
if (x > 0) {
return (double)(i + 1);
}
return (double)i;
}
@@ -145,8 +149,9 @@ double ln(double x)
double logx(double x, double y)
{
// Base may not equal 1 or be negative.
if (y == 1.f || y < 0.f || ln(y) == 0.f)
if (y == 1.f || y < 0.f || ln(y) == 0.f) {
return 0.f;
}
return ln(x) / ln(y);
}
+36 -21
View File
@@ -4,13 +4,13 @@
/// See LICENSE.md for details.
#include "pwd.h"
#include "sys/unistd.h"
#include "sys/errno.h"
#include "io/debug.h"
#include "assert.h"
#include "string.h"
#include "stdio.h"
#include "fcntl.h"
#include "io/debug.h"
#include "stdio.h"
#include "string.h"
#include "sys/errno.h"
#include "sys/unistd.h"
/// @brief Parses the input buffer and fills pwd with its details.
/// @param pwd the structure we need to fill.
@@ -20,32 +20,40 @@ static inline void __parse_line(passwd_t *pwd, char *buf)
assert(pwd && "Received null pwd!");
char *token, *ch;
// Parse the username.
if ((token = strtok(buf, ":")) != NULL)
if ((token = strtok(buf, ":")) != NULL) {
pwd->pw_name = token;
}
// Parse the password.
if ((token = strtok(NULL, ":")) != NULL)
if ((token = strtok(NULL, ":")) != NULL) {
pwd->pw_passwd = token;
}
// Parse the user ID.
if ((token = strtok(NULL, ":")) != NULL)
if ((token = strtok(NULL, ":")) != NULL) {
pwd->pw_uid = atoi(token);
}
// Parse the group ID.
if ((token = strtok(NULL, ":")) != NULL)
if ((token = strtok(NULL, ":")) != NULL) {
pwd->pw_gid = atoi(token);
}
// Parse the user information.
if ((token = strtok(NULL, ":")) != NULL)
if ((token = strtok(NULL, ":")) != NULL) {
pwd->pw_gecos = token;
}
// Parse the dir.
if ((token = strtok(NULL, ":")) != NULL)
if ((token = strtok(NULL, ":")) != NULL) {
pwd->pw_dir = token;
}
// Parse the shell.
if ((token = strtok(NULL, ":")) != NULL) {
pwd->pw_shell = token;
// Find carriege return.
if ((ch = strchr(pwd->pw_shell, '\r')))
if ((ch = strchr(pwd->pw_shell, '\r'))) {
*ch = 0;
}
// Find newline.
if ((ch = strchr(pwd->pw_shell, '\n')))
if ((ch = strchr(pwd->pw_shell, '\n'))) {
*ch = 0;
}
}
}
@@ -71,7 +79,7 @@ ssize_t __readline(int fd, char *buffer, size_t buflen)
}
}
}
long newline_len = (int)(newline - buffer);
long newline_len = (newline - buffer);
if (newline_len <= 0) {
return 0;
}
@@ -106,17 +114,20 @@ static inline char *__search_entry(int fd, char *buffer, int buflen, const char
} else {
// Name
char *ptr = strchr(buffer, ':');
if (ptr == NULL)
if (ptr == NULL) {
continue;
}
// Password
++ptr;
char *uid_start = strchr(ptr, ':');
if (uid_start == NULL)
if (uid_start == NULL) {
continue;
}
++uid_start;
ptr = strchr(uid_start, ':');
if (ptr == NULL)
if (ptr == NULL) {
continue;
}
*ptr = '\0';
// Parse the uid.
int found_uid = atoi(uid_start);
@@ -132,13 +143,15 @@ static inline char *__search_entry(int fd, char *buffer, int buflen, const char
passwd_t *getpwnam(const char *name)
{
if (name == NULL)
if (name == NULL) {
return NULL;
}
static passwd_t pwd;
static char buffer[BUFSIZ];
passwd_t *result;
if (!getpwnam_r(name, &pwd, buffer, BUFSIZ, &result))
if (!getpwnam_r(name, &pwd, buffer, BUFSIZ, &result)) {
return NULL;
}
return &pwd;
}
@@ -147,15 +160,17 @@ passwd_t *getpwuid(uid_t uid)
static passwd_t pwd;
static char buffer[BUFSIZ];
passwd_t *result;
if (!getpwuid_r(uid, &pwd, buffer, BUFSIZ, &result))
if (!getpwuid_r(uid, &pwd, buffer, BUFSIZ, &result)) {
return NULL;
}
return &pwd;
}
int getpwnam_r(const char *name, passwd_t *pwd, char *buf, size_t buflen, passwd_t **result)
{
if (name == NULL)
if (name == NULL) {
return 0;
}
int fd = open("/etc/passwd", O_RDONLY, 0);
if (fd == -1) {
pr_debug("Cannot open `/etc/passwd`\n");
+1 -1
View File
@@ -4,8 +4,8 @@
/// See LICENSE.md for details.
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "sched.h"
#include "sys/errno.h"
_syscall2(int, sched_setparam, pid_t, pid, const sched_param_t *, param)
+11 -7
View File
@@ -3,10 +3,10 @@
/// @copyright (c) 2014-2023 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include <assert.h>
#include "sys/errno.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include <assert.h>
char **environ;
@@ -23,9 +23,11 @@ static inline int __find_entry(const char *name, const size_t name_len)
{
if (environ) {
int index = 0;
for (char **ptr = environ; *ptr; ++ptr, ++index)
if (!strncmp((*ptr), name, name_len) && (*ptr)[name_len] == '=')
for (char **ptr = environ; *ptr; ++ptr, ++index) {
if (!strncmp((*ptr), name, name_len) && (*ptr)[name_len] == '=') {
return index;
}
}
}
return -1;
}
@@ -98,8 +100,9 @@ int setenv(const char *name, const char *value, int replace)
environ = __environ = new_environ;
}
// Free the previous entry.
if (environ[index])
if (environ[index]) {
free(environ[index]);
}
// Allocate the new entry.
environ[index] = malloc(total_len);
// Memcopy because we do not want the null terminating character.
@@ -129,8 +132,9 @@ int unsetenv(const char *name)
if (!strncmp(*ep, name, len) && (*ep)[len] == '=') {
/* Found it. Remove this pointer by moving later ones back. */
char **dp = ep;
do dp[0] = dp[1];
while (*dp++);
do {
dp[0] = dp[1];
} while (*dp++);
/* Continue the loop in case NAME appears again. */
} else {
++ep;
+4 -3
View File
@@ -4,10 +4,10 @@
/// See LICENSE.md for details.
#include "stddef.h"
#include "system/syscall_types.h"
#include "assert.h"
#include "stdlib.h"
#include "string.h"
#include "assert.h"
#include "system/syscall_types.h"
/// @brief Number which identifies a memory area allocated through a call to
/// malloc(), calloc() or realloc().
@@ -37,8 +37,9 @@ void *malloc(unsigned int size)
void *calloc(size_t num, size_t size)
{
void *ptr = malloc(num * size);
if (ptr)
if (ptr) {
memset(ptr, 0, num * size);
}
return ptr;
}
+20 -13
View File
@@ -5,9 +5,9 @@
#include "string.h"
#include "ctype.h"
#include "fcntl.h"
#include "stdio.h"
#include "stdlib.h"
#include "fcntl.h"
#ifdef __KERNEL__
#include "mem/kheap.h"
@@ -25,8 +25,9 @@ char *strncpy(char *destination, const char *source, size_t num)
// is found before num characters have been copied, destination is padded
// with zeros until a total of num characters have been written to it.
if (num) {
while (--num)
while (--num) {
*destination++ = '\0';
}
}
}
// Pointer to destination is returned.
@@ -35,8 +36,9 @@ char *strncpy(char *destination, const char *source, size_t num)
int strncmp(const char *s1, const char *s2, size_t n)
{
if (!n)
if (!n) {
return 0;
}
while ((--n > 0) && (*s1) && (*s2) && (*s1 == *s2)) {
s1++;
s2++;
@@ -74,8 +76,9 @@ char *strchr(const char *s, int ch)
while (*s && *s != (char)ch) {
s++;
}
if (*s == (char)ch)
if (*s == (char)ch) {
return (char *)s;
}
{
return NULL;
}
@@ -163,8 +166,9 @@ size_t strcspn(const char *string, const char *control)
size_t n;
// Clear out bit map.
for (n = 0; n < 32; n++)
for (n = 0; n < 32; n++) {
map[n] = 0;
}
// Set bits in control map.
while (*ctrl) {
@@ -192,8 +196,9 @@ char *strpbrk(const char *string, const char *control)
int n;
// Clear out bit map.
for (n = 0; n < 32; n++)
for (n = 0; n < 32; n++) {
map[n] = 0;
}
// Set bits in control map.
while (*ctrl) {
@@ -317,8 +322,9 @@ char *strncat(char *s1, const char *s2, size_t n)
s1--;
while (n--) {
if (!(*s1++ = *s2++))
if (!(*s1++ = *s2++)) {
return start;
}
}
*s1 = '\0';
@@ -397,11 +403,10 @@ char *strtok_r(char *str, const char *delim, char **saveptr)
*saveptr = s;
// Determine if a token has been found.
if (str == (char *)s) {
if (str == s) {
return NULL;
} else {
return str;
}
return str;
}
// Intrinsic functions.
@@ -453,7 +458,7 @@ void *memcpy(void *dst, const void *src, size_t num)
// Initialize the content of the memory.
while (num--) *_dst++ = *_src++;
// Return the pointer.
return (void *)dst;
return dst;
}
void *memccpy(void *dst, const void *src, int c, size_t n)
@@ -625,8 +630,9 @@ char *strdup(const char *s)
#else
char *new = malloc(len);
#endif
if (new == NULL)
if (new == NULL) {
return NULL;
}
new[len] = '\0';
return (char *)memcpy(new, s, len);
}
@@ -639,8 +645,9 @@ char *strndup(const char *s, size_t n)
#else
char *new = malloc(len);
#endif
if (new == NULL)
if (new == NULL) {
return NULL;
}
new[len] = '\0';
return (char *)memcpy(new, s, len);
}
+17 -14
View File
@@ -4,21 +4,21 @@
/// See LICENSE.md for details.
#include "sys/ipc.h"
#include "sys/unistd.h"
#include "sys/errno.h"
#include "sys/stat.h"
#include "sys/sem.h"
#include "sys/shm.h"
#include "sys/msg.h"
#include "system/syscall_types.h"
#include "stddef.h"
#include "string.h"
#include "io/debug.h"
#include "io/debug.h"
#include "stddef.h"
#include "stdio.h"
#include "stdio.h"
#include "stdlib.h"
#include "io/debug.h"
#include "stdio.h"
#include "string.h"
#include "sys/errno.h"
#include "sys/msg.h"
#include "sys/sem.h"
#include "sys/shm.h"
#include "sys/stat.h"
#include "sys/unistd.h"
#include "system/syscall_types.h"
_syscall3(void *, shmat, int, shmid, const void *, shmaddr, int, shmflg)
@@ -41,8 +41,9 @@ int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg)
long __res;
do {
__inline_syscall4(__res, msgsnd, msqid, msgp, msgsz, msgflg);
if (!(msgflg & IPC_NOWAIT) && (__res == -EAGAIN))
if (!(msgflg & IPC_NOWAIT) && (__res == -EAGAIN)) {
continue;
}
break;
} while (1);
__syscall_return(int, __res);
@@ -53,8 +54,9 @@ ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
long __res;
do {
__inline_syscall5(__res, msgrcv, msqid, msgp, msgsz, msgtyp, msgflg);
if (!(msgflg & IPC_NOWAIT) && ((__res == -EAGAIN) || (__res == -ENOMSG)))
if (!(msgflg & IPC_NOWAIT) && ((__res == -EAGAIN) || (__res == -ENOMSG))) {
continue;
}
break;
} while (1);
__syscall_return(int, __res);
@@ -92,8 +94,9 @@ long semop(int semid, struct sembuf *sops, unsigned nsops)
// If we get an error, the operation has been taken care of we stop
// the loop. We also stop the loop if the operation is not allowed
// and the IPC_NOWAIT flag is 1
if ((__res != -EAGAIN) || (op->sem_flg & IPC_NOWAIT))
if ((__res != -EAGAIN) || (op->sem_flg & IPC_NOWAIT)) {
break;
}
}
// If the operation couldn't be performed and we had the IPC_NOWAIT set
// to 1 then we return.
+2 -2
View File
@@ -5,9 +5,9 @@
/// See LICENSE.md for details.
#include "sys/mman.h"
#include "system/syscall_types.h"
#include "sys/unistd.h"
#include "sys/errno.h"
#include "sys/unistd.h"
#include "system/syscall_types.h"
_syscall6(void *, mmap, void *, addr, size_t, length, int, prot, int, flags, int, fd, off_t, offset)
+1 -1
View File
@@ -4,9 +4,9 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "string.h"
#include "limits.h"
#include "stdio.h"
#include "string.h"
#if 0
#include "vfs.h"
+2 -2
View File
@@ -4,8 +4,8 @@
/// See LICENSE.md for details.
#include "sys/utsname.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "stddef.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall1(int, uname, utsname_t *, buf)
+4 -4
View File
@@ -4,9 +4,9 @@
/// See LICENSE.md for details.
#include "termios.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "bits/ioctls.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
int tcgetattr(int fd, termios_t *termios_p)
{
@@ -16,7 +16,7 @@ int tcgetattr(int fd, termios_t *termios_p)
: "0"(__NR_ioctl), "b"((long)(fd)), "c"((long)(TCGETS)),
"d"((long)(termios_p)));
if (retval < 0) {
errno = -retval;
errno = -retval;
retval = -1;
}
return retval;
@@ -30,7 +30,7 @@ int tcsetattr(int fd, int optional_actions, const termios_t *termios_p)
: "0"(__NR_ioctl), "b"((long)(fd)), "c"((long)(TCSETS)),
"d"((long)(termios_p)));
if (retval < 0) {
errno = -retval;
errno = -retval;
retval = -1;
}
return retval;
+7 -7
View File
@@ -4,10 +4,10 @@
/// See LICENSE.md for details.
#include "time.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "string.h"
#include "stdio.h"
#include "string.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
/// @brief List of week days name.
static const char *weekdays[] = {
@@ -23,7 +23,7 @@ static const char *months[] = {
/// @brief Time function.
_syscall1(time_t, time, time_t *, t)
time_t difftime(time_t time1, time_t time2)
time_t difftime(time_t time1, time_t time2)
{
return time1 - time2;
}
@@ -69,7 +69,7 @@ tm_t *localtime(const time_t *time)
t /= 24;
// Convert Unix time to date
a = (unsigned int)((4 * t + 102032) / 146097 + 15);
b = (unsigned int)(t + 2442113 + a - (a / 4));
b = (t + 2442113 + a - (a / 4));
c = (20 * b - 2442) / 7305;
d = b - 365 * c - (c / 4);
e = d * 1000 / 30601;
@@ -383,7 +383,7 @@ size_t strftime(char *str, size_t maxsize, const char *format, const tm_t *timep
/// @brief nanosleep function.
_syscall2(int, nanosleep, const timespec *, req, timespec *, rem)
unsigned int sleep(unsigned int seconds)
unsigned int sleep(unsigned int seconds)
{
timespec req, rem;
req.tv_sec = seconds;
@@ -399,4 +399,4 @@ unsigned int sleep(unsigned int seconds)
_syscall2(int, getitimer, int, which, itimerval *, curr_value)
_syscall3(int, setitimer, int, which, const itimerval *, new_value, itimerval *, old_value)
_syscall3(int, setitimer, int, which, const itimerval *, new_value, itimerval *, old_value)
+1 -1
View File
@@ -4,8 +4,8 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall1(int, chdir, const char *, path)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall1(int, close, int, fd)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall2(int, creat, const char *, pathname, mode_t, mode)
+14 -10
View File
@@ -4,14 +4,14 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "io/debug.h"
#include "stdarg.h"
#include "stdlib.h"
#include "string.h"
#include "stdarg.h"
#include "fcntl.h"
#include "sys/errno.h"
#include "sys/stat.h"
#include "system/syscall_types.h"
extern char **environ;
@@ -43,7 +43,7 @@ static inline int __find_in_path(const char *file, char *buf, size_t buf_len)
strcat(buf, file);
if (stat(buf, &stat_buf) == 0) {
if (stat_buf.st_mode & S_IXUSR) {
// TODO: Check why `init` has problems with this free.
// TODO(enrico): Check why `init` has problems with this free.
// To reproduce the problem use this in init.c:
// execvp("login", _argv);
free(path);
@@ -128,8 +128,9 @@ int execl(const char *path, const char *arg, ...)
char *argv[argc + 1];
va_start(ap, arg);
argv[0] = (char *)arg;
for (int i = 1; i <= argc; ++i)
for (int i = 1; i <= argc; ++i) {
argv[i] = va_arg(ap, char *);
}
va_end(ap);
// Now, we can call `execve` plain and simple.
@@ -156,8 +157,9 @@ int execlp(const char *file, const char *arg, ...)
char *argv[argc + 1];
va_start(ap, arg);
argv[0] = (char *)arg;
for (int i = 1; i < argc; ++i)
for (int i = 1; i < argc; ++i) {
argv[i] = va_arg(ap, char *);
}
va_end(ap);
// Close argv.
@@ -193,8 +195,9 @@ int execle(const char *path, const char *arg, ...)
char *argv[argc + 1];
va_start(ap, arg);
argv[0] = (char *)arg;
for (int i = 1; i < argc; ++i)
for (int i = 1; i < argc; ++i) {
argv[i] = va_arg(ap, char *);
}
// Close argv.
argv[argc] = NULL;
@@ -232,8 +235,9 @@ int execlpe(const char *file, const char *arg, ...)
char *argv[argc + 1];
va_start(ap, arg);
argv[0] = (char *)arg;
for (int i = 1; i < argc; ++i)
for (int i = 1; i < argc; ++i) {
argv[i] = va_arg(ap, char *);
}
// Close argv.
argv[argc] = NULL;
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall0(pid_t, fork)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall2(char *, getcwd, char *, buf, size_t, size)
+1 -1
View File
@@ -4,8 +4,8 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall3(ssize_t, getdents, int, fd, dirent_t *, dirp, unsigned int, count)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall0(pid_t, getgid)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall1(pid_t, getpgid, pid_t, pid)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall0(pid_t, getpid)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall0(pid_t, getppid)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall1(pid_t, getsid, pid_t, pid)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall0(uid_t, getuid)
+2 -2
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall1(int, alarm, int, seconds)
_syscall1(unsigned, alarm, int, seconds)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall2(int, kill, pid_t, pid, int, sig)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall3(off_t, lseek, int, fd, off_t, offset, int, whence)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall2(int, mkdir, const char *, path, mode_t, mode)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall1(int, nice, int, inc)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall3(int, open, const char *, pathname, int, flags, mode_t, mode)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall3(ssize_t, read, int, fd, void *, buf, size_t, nbytes)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd, void *, arg)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall1(int, rmdir, const char *, path)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall1(int, setgid, pid_t, pid)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall2(int, setpgid, pid_t, pid, pid_t, pgid)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall0(pid_t, setsid)
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall1(int, setuid, uid_t, pid)
+5 -3
View File
@@ -4,8 +4,8 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
#include "signal.h"
#include "sys/bitops.h"
@@ -68,8 +68,9 @@ int sigaction(int signum, const sigaction_t *act, sigaction_t *oldact)
const char *strsignal(int sig)
{
if ((sig >= SIGHUP) && (sig < NSIG))
if ((sig >= SIGHUP) && (sig < NSIG)) {
return sys_siglist[sig - 1];
}
return NULL;
}
@@ -111,7 +112,8 @@ int sigdelset(sigset_t *set, int signum)
int sigismember(sigset_t *set, int signum)
{
if (set)
if (set) {
return bit_check(set->sig[(signum - 1) / 32], (signum - 1) % 32);
}
return -1;
}
+1 -1
View File
@@ -4,8 +4,8 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
#include "sys/stat.h"
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall1(int, unlink, const char *, path)
+10 -6
View File
@@ -4,13 +4,13 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "sys/wait.h"
#include "sys/errno.h"
#include "sys/wait.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
pid_t waitpid(pid_t pid, int *status, int options)
{
@@ -18,15 +18,19 @@ pid_t waitpid(pid_t pid, int *status, int options)
int __status = 0;
do {
__inline_syscall3(__res, waitpid, pid, &__status, options);
if (__res < 0)
if (__res < 0) {
break;
if (__status == EXIT_ZOMBIE)
}
if (__status == EXIT_ZOMBIE) {
break;
if (options && WNOHANG)
}
if (options && WNOHANG) {
break;
}
} while (1);
if (status)
if (status) {
*status = __status;
}
__syscall_return(pid_t, __res);
}
+1 -1
View File
@@ -4,7 +4,7 @@
/// See LICENSE.md for details.
#include "sys/unistd.h"
#include "system/syscall_types.h"
#include "sys/errno.h"
#include "system/syscall_types.h"
_syscall3(ssize_t, write, int, fd, const void *, buf, size_t, nbytes)
+37 -25
View File
@@ -3,9 +3,9 @@
/// @copyright (c) 2014-2023 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <sys/unistd.h>
/// @brief Read formatted data from string.
@@ -21,18 +21,19 @@ static int __vsscanf(const char *buf, const char *s, va_list ap)
char tmp[BUFSIZ];
while (*s && *buf) {
while (isspace(*s))
while (isspace(*s)) {
++s;
}
if (*s == '%') {
++s;
for (; *s; ++s) {
if (strchr("dibouxcsefg%", *s))
if (strchr("dibouxcsefg%", *s)) {
break;
if (*s == '*')
}
if (*s == '*') {
noassign = 1;
else if (isdigit(*s)) {
for (tc = s; isdigit(*s); ++s)
{}
} else if (isdigit(*s)) {
for (tc = s; isdigit(*s); ++s) {}
strncpy(tmp, tc, s - tc);
tmp[s - tc] = '\0';
width = strtol(tmp, NULL, 10);
@@ -40,10 +41,12 @@ static int __vsscanf(const char *buf, const char *s, va_list ap)
}
}
if (*s == 's') {
while (isspace(*buf))
while (isspace(*buf)) {
++buf;
if (!width)
}
if (!width) {
width = strcspn(buf, " \t\n\r\f\v");
}
if (!noassign) {
char *string = va_arg(ap, char *);
strncpy(string, buf, width);
@@ -51,48 +54,56 @@ static int __vsscanf(const char *buf, const char *s, va_list ap)
}
buf += width;
} else if (*s == 'c') {
while (isspace(*buf))
while (isspace(*buf)) {
++buf;
if (!width)
}
if (!width) {
width = 1;
}
if (!noassign) {
strncpy(va_arg(ap, char *), buf, width);
}
buf += width;
} else if (strchr("duxob", *s)) {
while (isspace(*buf))
while (isspace(*buf)) {
++buf;
if (*s == 'd' || *s == 'u')
}
if (*s == 'd' || *s == 'u') {
base = 10;
else if (*s == 'x')
} else if (*s == 'x') {
base = 16;
else if (*s == 'o')
} else if (*s == 'o') {
base = 8;
else if (*s == 'b')
} else if (*s == 'b') {
base = 2;
}
if (!width) {
if (isspace(*(s + 1)) || *(s + 1) == 0)
if (isspace(*(s + 1)) || *(s + 1) == 0) {
width = strcspn(buf, " \t\n\r\f\v");
else
} else {
width = strchr(buf, *(s + 1)) - buf;
}
}
strncpy(tmp, buf, width);
tmp[width] = '\0';
buf += width;
if (!noassign)
if (!noassign) {
*va_arg(ap, unsigned int *) = strtol(tmp, NULL, base);
}
}
if (!noassign)
if (!noassign) {
++count;
}
width = noassign = 0;
++s;
} else {
while (isspace(*buf))
while (isspace(*buf)) {
++buf;
if (*s != *buf)
}
if (*s != *buf) {
break;
else
++s, ++buf;
}
++s, ++buf;
}
}
return (count);
@@ -109,8 +120,9 @@ static int __vfscanf(int fd, const char *fmt, va_list ap)
int count;
char buf[BUFSIZ + 1];
if (fgets(buf, BUFSIZ, fd) == 0)
if (fgets(buf, BUFSIZ, fd) == 0) {
return (-1);
}
count = __vsscanf(buf, fmt, ap);
return (count);
}
+20 -13
View File
@@ -4,12 +4,12 @@
/// See LICENSE.md for details.
#include "sys/bitops.h"
#include "fcvt.h"
#include "ctype.h"
#include "string.h"
#include "fcvt.h"
#include "stdarg.h"
#include "stdint.h"
#include "stdio.h"
#include "string.h"
#include "sys/unistd.h"
/// Size of the buffer used to call cvt functions.
@@ -35,8 +35,9 @@ static char *_upper_digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static inline int skip_atoi(const char **s)
{
int i = 0;
while (isdigit(**s))
while (isdigit(**s)) {
i = i * 10 + *((*s)++) - '0';
}
return i;
}
@@ -106,16 +107,17 @@ static char *number(char *str, long num, int base, int size, int32_t precision,
}
size -= precision;
if (!bitmask_check(flags, FLAGS_ZEROPAD | FLAGS_LEFT)) {
while (size-- > 0)
while (size-- > 0) {
*str++ = ' ';
}
}
if (sign) {
*str++ = sign;
}
if (bitmask_check(flags, FLAGS_HASH)) {
if (base == 8)
if (base == 8) {
*str++ = '0';
else if (base == 16) {
} else if (base == 16) {
*str++ = '0';
*str++ = _digits[33];
}
@@ -377,8 +379,9 @@ static char *flt(char *str, double num, int size, int precision, char fmt, unsig
int n, i;
// Left align means no zero padding.
if (bitmask_check(flags, FLAGS_LEFT))
if (bitmask_check(flags, FLAGS_LEFT)) {
bitmask_clear_assign(flags, FLAGS_ZEROPAD);
}
// Determine padding and sign char.
c = bitmask_check(flags, FLAGS_ZEROPAD) ? '0' : ' ';
@@ -589,10 +592,11 @@ int vsprintf(char *str, const char *fmt, va_list args)
bitmask_set_assign(flags, FLAGS_UPPERCASE);
break;
case 'a':
if (qualifier == 'l')
if (qualifier == 'l') {
tmp = eaddr(tmp, va_arg(args, unsigned char *), field_width, precision, flags);
else
} else {
tmp = iaddr(tmp, va_arg(args, unsigned char *), field_width, precision, flags);
}
continue;
// Integer number formats - set up the flags and "break".
case 'o':
@@ -621,12 +625,14 @@ int vsprintf(char *str, const char *fmt, va_list args)
tmp = flt(tmp, va_arg(args, double), field_width, precision, *fmt, bitmask_set(flags, FLAGS_SIGN));
continue;
default:
if (*fmt != '%')
if (*fmt != '%') {
*tmp++ = '%';
if (*fmt)
}
if (*fmt) {
*tmp++ = *fmt;
else
} else {
--fmt;
}
continue;
}
@@ -694,8 +700,9 @@ int fprintf(int fd, const char *fmt, ...)
va_end(ap);
if (len > 0) {
if (write(fd, buffer, len) <= 0)
if (write(fd, buffer, len) <= 0) {
return EOF;
}
return len;
}
return -1;
+1 -1
View File
@@ -44,7 +44,7 @@ void set_keymap_type(keymap_type_t type);
/// @brief Returns the current keymap for the given scancode.
/// @param scancode the scancode we want.
/// @return Pointer to the keymap.
const keymap_t *get_keymap(int scancode);
const keymap_t *get_keymap(unsigned int scancode);
/// @brief Initializes the supported keymaps.
void init_keymaps();
+1 -1
View File
@@ -176,7 +176,7 @@ int sys_nanosleep(const timespec *req, timespec *rem);
/// @return the number of seconds remaining until any previously scheduled
/// alarm was due to be delivered, or zero if there was no previously
/// scheduled alarm.
int sys_alarm(int seconds);
unsigned sys_alarm(int seconds);
/// @brief Rappresents a time value.
struct timeval {
+14 -11
View File
@@ -5,10 +5,10 @@
#include "boot.h"
#include "link_access.h"
#include "sys/module.h"
#include "mem/paging.h"
#include "elf/elf.h"
#include "link_access.h"
#include "mem/paging.h"
#include "sys/module.h"
/// @defgroup bootloader Bootloader
/// @brief Set of functions and variables for booting the kernel.
@@ -60,8 +60,9 @@ static inline void __debug_putchar(char c)
/// @param s the string to send to the debug port.
static inline void __debug_puts(char *s)
{
while ((*s) != 0)
while ((*s) != 0) {
__outportb(SERIAL_COM1, *s++);
}
}
/// @brief Align memory address to the specified value (round up).
@@ -93,12 +94,12 @@ static void __setup_pages(uint32_t pfn_virt_start, uint32_t pfn_phys_start, uint
uint32_t base_pgentry = pfn_virt_start % 1024;
uint32_t pg_offset = 0;
for (int i = base_pgtable; i < 1024 && pfn_count; i++) {
for (uint32_t i = base_pgtable; i < 1024 && pfn_count; i++) {
page_table_t *table = boot_pgtables + i;
uint32_t pgentry_start = (i == base_pgtable) ? base_pgentry : 0;
for (int j = pgentry_start; j < 1024 && pfn_count; j++, pfn_count--) {
for (uint32_t j = pgentry_start; j < 1024 && pfn_count; j++, pfn_count--) {
table->pages[j].frame = pfn_phys_start + pg_offset++;
table->pages[j].rw = 1;
table->pages[j].present = 1;
@@ -138,7 +139,7 @@ static void __get_kernel_low_high(elf_header_t *elf_hdr, uint32_t *virt_low, uin
// Prepare a pointer to a program header.
elf_program_header_t *program_header;
// Compute the offset for accessing the program headers.
uint32_t offset = (uint32_t)elf_hdr + (uint32_t)elf_hdr->phoff;
uint32_t offset = (uint32_t)elf_hdr + elf_hdr->phoff;
// In this two variables we will store the start and end addresses of the segment.
uint32_t segment_start, segment_end;
// Iterate for each program header.
@@ -183,7 +184,7 @@ static inline void __relocate_kernel_image(elf_header_t *elf_hdr)
// Get the elf file starting address.
kernel_start = (char *)elf_hdr;
// Compute the offset for accessing the program headers.
offset = (uint32_t)kernel_start + (uint32_t)elf_hdr->phoff;
offset = (uint32_t)kernel_start + elf_hdr->phoff;
// Iterate over the program headers.
for (int i = 0; i < elf_hdr->phnum; i++) {
// Get the program header.
@@ -191,18 +192,20 @@ static inline void __relocate_kernel_image(elf_header_t *elf_hdr)
// Get the virtual address of the program header.
virtual_address = (char *)program_header->vaddr;
// Get the physical address of the program header.
physical_address = (char *)(kernel_start + program_header->offset);
physical_address = (kernel_start + program_header->offset);
// Move only the loadable segments.
if (program_header->type == PT_LOAD) {
// Get the valid size of the segment by taking the minimum between
// the size in bytes of the segment in the file image, in memory.
valid_size = min(program_header->filesz, program_header->memsz);
// Copy the physical data of the image to the corresponding virtual address.
for (int j = 0; j < valid_size; j++)
for (uint32_t j = 0; j < valid_size; j++) {
virtual_address[j] = physical_address[j];
}
// Set to 0 parts not present in memory!
for (int j = valid_size; j < program_header->memsz; j++)
for (uint32_t j = valid_size; j < program_header->memsz; j++) {
virtual_address[j] = 0;
}
}
}
}
+31 -40
View File
@@ -11,18 +11,18 @@
#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level.
#include "io/debug.h" // Include debugging functions.
#include "drivers/keyboard/keyboard.h"
#include "io/port_io.h"
#include "hardware/pic8259.h"
#include "drivers/keyboard/keymap.h"
#include "sys/bitops.h"
#include "io/video.h"
#include "drivers/ps2.h"
#include "ctype.h"
#include "descriptor_tables/isr.h"
#include "drivers/keyboard/keyboard.h"
#include "drivers/keyboard/keymap.h"
#include "drivers/ps2.h"
#include "hardware/pic8259.h"
#include "io/port_io.h"
#include "io/video.h"
#include "process/scheduler.h"
#include "ring_buffer.h"
#include "string.h"
#include "sys/bitops.h"
/// Tracks the state of the leds.
static uint8_t ledstate = 0;
@@ -43,48 +43,37 @@ spinlock_t scancodes_lock;
#define KBD_LEFT_ALT (1 << 7) ///< Flag which identifies the left alt.
#define KBD_RIGHT_ALT (1 << 8) ///< Flag which identifies the right alt.
static inline bool_t get_keypad_number(int scancode)
static inline int get_keypad_number(unsigned int scancode)
{
if (scancode == KEY_KP0)
return 0;
if (scancode == KEY_KP1)
return 1;
if (scancode == KEY_KP2)
return 2;
if (scancode == KEY_KP3)
return 3;
if (scancode == KEY_KP4)
return 4;
if (scancode == KEY_KP5)
return 5;
if (scancode == KEY_KP6)
return 6;
if (scancode == KEY_KP7)
return 7;
if (scancode == KEY_KP8)
return 8;
if (scancode == KEY_KP9)
return 9;
if (scancode == KEY_KP0) { return 0; }
if (scancode == KEY_KP1) { return 1; }
if (scancode == KEY_KP2) { return 2; }
if (scancode == KEY_KP3) { return 3; }
if (scancode == KEY_KP4) { return 4; }
if (scancode == KEY_KP5) { return 5; }
if (scancode == KEY_KP6) { return 6; }
if (scancode == KEY_KP7) { return 7; }
if (scancode == KEY_KP8) { return 8; }
if (scancode == KEY_KP9) { return 9; }
return -1;
}
static inline void keyboard_push_front(int c)
static inline void keyboard_push_front(unsigned int c)
{
if (c >= 0) {
spinlock_lock(&scancodes_lock);
fs_rb_scancode_push_front(&scancodes, c);
spinlock_unlock(&scancodes_lock);
}
spinlock_lock(&scancodes_lock);
fs_rb_scancode_push_front(&scancodes, (int)c);
spinlock_unlock(&scancodes_lock);
}
int keyboard_pop_back()
{
int c;
spinlock_lock(&scancodes_lock);
if (!fs_rb_scancode_empty(&scancodes))
if (!fs_rb_scancode_empty(&scancodes)) {
c = fs_rb_scancode_pop_back(&scancodes);
else
} else {
c = -1;
}
spinlock_unlock(&scancodes_lock);
return c;
}
@@ -93,10 +82,11 @@ int keyboard_back()
{
int c;
spinlock_lock(&scancodes_lock);
if (!fs_rb_scancode_empty(&scancodes))
if (!fs_rb_scancode_empty(&scancodes)) {
c = fs_rb_scancode_back(&scancodes);
else
} else {
c = -1;
}
spinlock_unlock(&scancodes_lock);
return c;
}
@@ -105,10 +95,11 @@ int keyboard_front()
{
int c = -1;
spinlock_lock(&scancodes_lock);
if (!fs_rb_scancode_empty(&scancodes))
if (!fs_rb_scancode_empty(&scancodes)) {
c = fs_rb_scancode_front(&scancodes);
else
} else {
c = -1;
}
spinlock_unlock(&scancodes_lock);
return c;
}
+5 -3
View File
@@ -20,19 +20,21 @@ keymap_type_t get_keymap_type()
void set_keymap_type(keymap_type_t type)
{
if (type != keymap_type)
if (type != keymap_type) {
keymap_type = type;
}
}
const keymap_t *get_keymap(int scancode)
const keymap_t *get_keymap(unsigned int scancode)
{
return &keymaps[keymap_type][scancode];
}
void init_keymaps()
{
for (int i = 0; i < KEYMAP_TYPE_MAX; ++i)
for (int i = 0; i < KEYMAP_TYPE_MAX; ++i) {
memset(&keymaps[i], -1, sizeof(keymap_t));
}
// == ITALIAN KEY MAPPING =================================================
// Keys Normal Shifted Ctrl Alt
+7 -13
View File
@@ -11,8 +11,8 @@
#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level.
#include "io/debug.h" // Include debugging functions.
#include "drivers/mouse.h"
#include "descriptor_tables/isr.h"
#include "drivers/mouse.h"
#include "hardware/pic8259.h"
#include "io/port_io.h"
@@ -38,21 +38,15 @@ static void __mouse_waitcmd(unsigned char type)
{
register unsigned int _time_out = 100000;
if (type == 0) {
// DATA.
// DATA
while (_time_out--) {
if ((inportb(0x64) & 1) == 1) {
return;
}
if ((inportb(0x64) & 1) == 1) { break; }
}
return;
} else {
while (_time_out--) // SIGNALS
{
if ((inportb(0x64) & 2) == 0) {
return;
}
// SIGNALS
while (_time_out--) {
if ((inportb(0x64) & 2) == 0) { break; }
}
return;
}
}
@@ -207,7 +201,7 @@ int mouse_finalize()
{
// Uninstall the IRQ.
irq_uninstall_handler(IRQ_MOUSE, __mouse_isr);
// Disable the mouse.
__mouse_disable();
return 0;
+32 -17
View File
@@ -10,10 +10,10 @@
#include "io/debug.h" // Include debugging functions.
#include "drivers/ps2.h"
#include "proc_access.h"
#include "sys/bitops.h"
#include "io/port_io.h"
#include "proc_access.h"
#include "stdbool.h"
#include "sys/bitops.h"
#define PS2_DATA 0x60 ///< Data signal line.
#define PS2_STATUS 0x64 ///< Status signal line.
@@ -48,15 +48,17 @@
/// @brief Polling until we can receive bytes from the device.
static inline void __ps2_wait_read()
{
while (!bit_check(inportb(PS2_STATUS), 0))
while (!bit_check(inportb(PS2_STATUS), 0)) {
pause();
}
}
/// @brief Polling until we can send bytes to the device.
static inline void __ps2_wait_write()
{
while (bit_check(inportb(PS2_STATUS), 1))
while (bit_check(inportb(PS2_STATUS), 1)) {
pause();
}
}
void ps2_write(unsigned char data)
@@ -127,14 +129,18 @@ static inline void __ps2_write_second_port(unsigned char byte)
static const char *__ps2_get_response_error_message(unsigned response)
{
if (response == 0x01)
if (response == 0x01) {
return "clock line stuck low";
if (response == 0x02)
}
if (response == 0x02) {
return "clock line stuck high";
if (response == 0x03)
}
if (response == 0x03) {
return "data line stuck low";
if (response == 0x04)
}
if (response == 0x04) {
return "data line stuck high";
}
return "unknown error";
}
@@ -206,7 +212,8 @@ int ps2_initialize()
// Send 0xAA to the controller.
__ps2_write_command(PS2_CTRL_TEST_CONTROLLER);
// Read the response.
if ((response = ps2_read()) == PS2_TEST_FAIL1) {
response = ps2_read();
if (response == PS2_TEST_FAIL1) {
pr_err("Self-test failed : 0x%02x\n", response);
return 1;
}
@@ -226,7 +233,8 @@ int ps2_initialize()
// Read the status.
status = __ps2_get_controller_status();
// Check if it is a dual channel PS/2 device.
if ((dual = !bit_check(status, 5))) {
dual = !bit_check(status, 5);
if (dual) {
pr_debug("Recognized a `dual channel` PS/2 controller...\n");
__ps2_disable_second_port();
} else {
@@ -245,7 +253,8 @@ int ps2_initialize()
// can still keep using/supporting the other PS/2 port.
__ps2_write_command(PS2_CTRL_P1_TEST);
if ((response = ps2_read()) && (response >= 0x01) && (response <= 0x04)) {
response = ps2_read();
if (response && (response >= 0x01) && (response <= 0x04)) {
pr_err("Interface test failed on first port : %02x\n", response);
pr_err("Reason: %s.\n", __ps2_get_response_error_message(response));
return 1;
@@ -253,7 +262,8 @@ int ps2_initialize()
// If it is a dual channel, check the second port.
if (dual) {
__ps2_write_command(PS2_CTRL_P2_TEST);
if ((response = ps2_read()) && (response >= 0x01) && (response <= 0x04)) {
response = ps2_read();
if (response && (response >= 0x01) && (response <= 0x04)) {
pr_err("Interface test failed on second port : %02x\n", response);
pr_err("Reason: %s.\n", __ps2_get_response_error_message(response));
return 1;
@@ -271,8 +281,9 @@ int ps2_initialize()
// Enable the first port.
__ps2_enable_first_port();
// Enable the second port.
if (dual)
if (dual) {
__ps2_enable_second_port();
}
// Get the status.
status = __ps2_get_controller_status();
pr_debug("Status : %s (%3d | %02x)\n", dec_to_binary(status, 8), status, status);
@@ -298,12 +309,14 @@ int ps2_initialize()
// Reset first port.
__ps2_write_first_port(0xFF);
// Wait for `command acknowledged`.
if ((response = ps2_read()) != PS2_ACK) {
response = ps2_read();
if ((response) != PS2_ACK) {
pr_err("Failed to reset first PS/2 port: %d\n", response);
return 1;
}
// Wait for `self test successful`.
if ((response = ps2_read()) != PS2_TEST_SUCCESS) {
response = ps2_read();
if ((response) != PS2_TEST_SUCCESS) {
pr_err("Failed to reset first PS/2 port: %d\n", response);
return 1;
}
@@ -311,12 +324,14 @@ int ps2_initialize()
// Reset second port.
__ps2_write_second_port(0xFF);
// Wait for `command acknowledged`.
if ((response = ps2_read()) != PS2_ACK) {
response = ps2_read();
if ((response) != PS2_ACK) {
pr_err("Failed to reset first PS/2 port: %d\n", response);
return 1;
}
// Wait for `self test successful`.
if ((response = ps2_read()) != PS2_TEST_SUCCESS) {
response = ps2_read();
if ((response) != PS2_TEST_SUCCESS) {
pr_err("Failed to reset first PS/2 port: %d\n", response);
return 1;
}
+9 -16
View File
@@ -11,12 +11,12 @@
#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level.
#include "io/debug.h" // Include debugging functions.
#include "descriptor_tables/isr.h"
#include "drivers/rtc.h"
#include "hardware/pic8259.h"
#include "string.h"
#include "io/port_io.h"
#include "kernel.h"
#include "descriptor_tables/isr.h"
#include "string.h"
#define CMOS_ADDR 0x70 ///< Addess where we need to write the Address.
#define CMOS_DATA 0x71 ///< Addess where we need to write the Data.
@@ -30,20 +30,13 @@ int is_bcd;
static inline unsigned int rtc_are_different(tm_t *t0, tm_t *t1)
{
if (t0->tm_sec != t1->tm_sec)
return 1;
if (t0->tm_min != t1->tm_min)
return 1;
if (t0->tm_hour != t1->tm_hour)
return 1;
if (t0->tm_mon != t1->tm_mon)
return 1;
if (t0->tm_year != t1->tm_year)
return 1;
if (t0->tm_wday != t1->tm_wday)
return 1;
if (t0->tm_mday != t1->tm_mday)
return 1;
if (t0->tm_sec != t1->tm_sec) { return 1; }
if (t0->tm_min != t1->tm_min) { return 1; }
if (t0->tm_hour != t1->tm_hour) { return 1; }
if (t0->tm_mon != t1->tm_mon) { return 1; }
if (t0->tm_year != t1->tm_year) { return 1; }
if (t0->tm_wday != t1->tm_wday) { return 1; }
if (t0->tm_mday != t1->tm_mday) { return 1; }
return 0;
}
+99 -76
View File
@@ -9,18 +9,18 @@
#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level.
#include "io/debug.h" // Include debugging functions.
#include "fs/ext2.h"
#include "process/scheduler.h"
#include "process/process.h"
#include "klib/spinlock.h"
#include "fs/vfs_types.h"
#include "sys/errno.h"
#include "fs/vfs.h"
#include "assert.h"
#include "libgen.h"
#include "string.h"
#include "stdio.h"
#include "fcntl.h"
#include "fs/ext2.h"
#include "fs/vfs.h"
#include "fs/vfs_types.h"
#include "klib/spinlock.h"
#include "libgen.h"
#include "process/process.h"
#include "process/scheduler.h"
#include "stdio.h"
#include "string.h"
#include "sys/errno.h"
#define EXT2_SUPERBLOCK_MAGIC 0xEF53 ///< Magic value used to identify an ext2 filesystem.
#define EXT2_INDIRECT_BLOCKS 12 ///< Amount of indirect blocks in an inode.
@@ -431,39 +431,25 @@ static const char *uuid_to_string(uint8_t uuid[16])
/// @return the string representing the ext2_file_type.
static const char *ext2_file_type_to_string(ext2_file_type_t ext2_type)
{
if (ext2_type == ext2_file_type_regular_file)
return "REG";
if (ext2_type == ext2_file_type_directory)
return "DIR";
if (ext2_type == ext2_file_type_character_device)
return "CHR";
if (ext2_type == ext2_file_type_block_device)
return "BLK";
if (ext2_type == ext2_file_type_named_pipe)
return "FIFO";
if (ext2_type == ext2_file_type_socket)
return "SOCK";
if (ext2_type == ext2_file_type_symbolic_link)
return "LNK";
if (ext2_type == ext2_file_type_regular_file) { return "REG"; }
if (ext2_type == ext2_file_type_directory) { return "DIR"; }
if (ext2_type == ext2_file_type_character_device) { return "CHR"; }
if (ext2_type == ext2_file_type_block_device) { return "BLK"; }
if (ext2_type == ext2_file_type_named_pipe) { return "FIFO"; }
if (ext2_type == ext2_file_type_socket) { return "SOCK"; }
if (ext2_type == ext2_file_type_symbolic_link) { return "LNK"; }
return "UNK";
}
static int ext2_file_type_to_vfs_file_type(int ext2_type)
{
if (ext2_type == ext2_file_type_regular_file)
return DT_REG;
if (ext2_type == ext2_file_type_directory)
return DT_DIR;
if (ext2_type == ext2_file_type_character_device)
return DT_CHR;
if (ext2_type == ext2_file_type_block_device)
return DT_BLK;
if (ext2_type == ext2_file_type_named_pipe)
return DT_FIFO;
if (ext2_type == ext2_file_type_socket)
return DT_SOCK;
if (ext2_type == ext2_file_type_symbolic_link)
return DT_LNK;
if (ext2_type == ext2_file_type_regular_file) { return DT_REG; }
if (ext2_type == ext2_file_type_directory) { return DT_DIR; }
if (ext2_type == ext2_file_type_character_device) { return DT_CHR; }
if (ext2_type == ext2_file_type_block_device) { return DT_BLK; }
if (ext2_type == ext2_file_type_named_pipe) { return DT_FIFO; }
if (ext2_type == ext2_file_type_socket) { return DT_SOCK; }
if (ext2_type == ext2_file_type_symbolic_link) { return DT_LNK; }
return DT_UNKNOWN;
}
@@ -488,14 +474,17 @@ static inline int __ext2_valid_permissions(
const int oth)
{
// The task is the owner.
if ((mask & usr) && (task->uid == uid))
if ((mask & usr) && (task->uid == uid)) {
return 1;
}
// The task belongs to the correct group.
if ((mask & grp) && (task->gid == gid))
if ((mask & grp) && (task->gid == gid)) {
return 1;
}
// The task is not the owner and does not belong to the correct group.
if ((mask & oth) && (task->uid != uid) && (task->gid != gid))
if ((mask & oth) && (task->uid != uid) && (task->gid != gid)) {
return 1;
}
return 0;
}
@@ -620,8 +609,9 @@ static void ext2_dump_inode(ext2_inode_t *inode)
pr_debug(" Links : %2u Flags : %d\n", inode->links_count, inode->flags);
pr_debug(" Blocks : [ ");
for (int i = 0; i < EXT2_INDIRECT_BLOCKS; ++i) {
if (inode->data.blocks.dir_blocks[i])
if (inode->data.blocks.dir_blocks[i]) {
pr_debug("%u ", inode->data.blocks.dir_blocks[i]);
}
}
pr_debug("]\n");
pr_debug("IBlocks : %u\n", inode->data.blocks.indir_block);
@@ -662,8 +652,9 @@ static void ext2_dump_bgdt(ext2_filesystem_t *fs)
ext2_read_block(fs, gd->block_bitmap, cache);
pr_debug(" Block Bitmap at %u\n", gd->block_bitmap);
for (uint32_t j = 0; j < fs->block_size; ++j) {
if ((j % 8) == 0)
if ((j % 8) == 0) {
pr_debug(" Block index: %4u, Bitmap: %s\n", j / 8, dec_to_binary(cache[j / 8], 8));
}
if (!ext2_check_bitmap_bit(cache, j)) {
pr_debug(" First free block in group is in block %u, the linear index is %u\n", j / 8, j);
break;
@@ -673,8 +664,9 @@ static void ext2_dump_bgdt(ext2_filesystem_t *fs)
ext2_read_block(fs, gd->inode_bitmap, cache);
pr_debug(" Inode Bitmap at %d\n", gd->inode_bitmap);
for (uint32_t j = 0; j < fs->block_size; ++j) {
if ((j % 8) == 0)
if ((j % 8) == 0) {
pr_debug(" Block index: %4d, Bitmap: %s\n", j / 8, dec_to_binary(cache[j / 8], 8));
}
if (!ext2_check_bitmap_bit(cache, j)) {
pr_debug(" First free block in group is in block %d, the linear index is %d\n", j / 8, j);
break;
@@ -755,10 +747,11 @@ static ext2_block_status_t ext2_check_bitmap_bit(uint8_t *buffer, uint32_t linea
/// @param status the new status of the block (free|occupied).
static void ext2_set_bitmap_bit(uint8_t *buffer, uint32_t linear_index, ext2_block_status_t status)
{
if (status == ext2_block_status_occupied)
if (status == ext2_block_status_occupied) {
bit_set_assign(buffer[linear_index / 8], linear_index % 8);
else
} else {
bit_clear_assign(buffer[linear_index / 8], linear_index % 8);
}
}
/// @brief Searches for a free inode inside the group data loaded inside the cache.
@@ -775,11 +768,13 @@ static inline int ext2_find_free_inode_in_group(
for ((*linear_index) = 0; (*linear_index) < fs->superblock.inodes_per_group; ++(*linear_index)) {
// If we need to skip the reserved inodes, we skip the round if the
// index is that of a reserved inode (superblock.first_ino).
if (skip_reserved && ((*linear_index) < fs->superblock.first_ino))
if (skip_reserved && ((*linear_index) < fs->superblock.first_ino)) {
continue;
}
// Check if the entry is free.
if (!ext2_check_bitmap_bit(cache, *linear_index))
if (!ext2_check_bitmap_bit(cache, *linear_index)) {
return 1;
}
}
return 0;
}
@@ -803,8 +798,9 @@ static inline int ext2_find_free_inode(
(*group_index) = preferred_group;
// Find the first free inode. We need to ask to skip reserved inodes,
// only if we are in group 0.
if (ext2_find_free_inode_in_group(fs, cache, linear_index, (*group_index) == 0))
if (ext2_find_free_inode_in_group(fs, cache, linear_index, (*group_index) == 0)) {
return 1;
}
}
// Get the group and bit index of the first free block.
for ((*group_index) = 0; (*group_index) < fs->block_groups_count; ++(*group_index)) {
@@ -817,8 +813,9 @@ static inline int ext2_find_free_inode(
}
// Find the first free inode. We need to ask to skip reserved
// inodes, only if we are in group 0.
if (ext2_find_free_inode_in_group(fs, cache, linear_index, (*group_index) == 0))
if (ext2_find_free_inode_in_group(fs, cache, linear_index, (*group_index) == 0)) {
return 1;
}
}
}
return 0;
@@ -834,8 +831,9 @@ static inline int ext2_find_free_block_in_group(ext2_filesystem_t *fs, uint8_t *
{
for ((*linear_index) = 0; (*linear_index) < fs->superblock.blocks_per_group; ++(*linear_index)) {
// Check if the entry is free.
if (!ext2_check_bitmap_bit(cache, *linear_index))
if (!ext2_check_bitmap_bit(cache, *linear_index)) {
return 1;
}
}
return 0;
}
@@ -861,8 +859,9 @@ static inline int ext2_find_free_block(
return 0;
}
// Find the first free block.
if (ext2_find_free_block_in_group(fs, cache, linear_index))
if (ext2_find_free_block_in_group(fs, cache, linear_index)) {
return 1;
}
}
}
return 0;
@@ -929,8 +928,9 @@ static int ext2_read_bgdt(ext2_filesystem_t *fs)
{
pr_debug("Read BGDT for EXT2 filesystem (0x%x)\n", fs);
if (fs->block_groups) {
for (uint32_t i = 0; i < fs->bgdt_length; ++i)
for (uint32_t i = 0; i < fs->bgdt_length; ++i) {
ext2_read_block(fs, fs->bgdt_start_block + i, (uint8_t *)((uintptr_t)fs->block_groups + (fs->block_size * i)));
}
return 0;
}
pr_err("The `block_groups` list is not initialized.\n");
@@ -944,8 +944,9 @@ static int ext2_write_bgdt(ext2_filesystem_t *fs)
{
pr_debug("Write BGDT for EXT2 filesystem (0x%x)\n", fs);
if (fs->block_groups) {
for (uint32_t i = 0; i < fs->bgdt_length; ++i)
for (uint32_t i = 0; i < fs->bgdt_length; ++i) {
ext2_write_block(fs, fs->bgdt_start_block + i, (uint8_t *)((uintptr_t)fs->block_groups + (fs->block_size * i)));
}
return 0;
}
pr_err("The `block_groups` list is not initialized.\n");
@@ -1149,13 +1150,15 @@ static int ext2_set_real_block_index(ext2_filesystem_t *fs, ext2_inode_t *inode,
if (!inode->data.blocks.indir_block) {
// Allocate a new block.
uint32_t new_block_index = ext2_allocate_block(fs);
if (new_block_index == 0)
if (new_block_index == 0) {
return -1;
}
// Update the index.
inode->data.blocks.indir_block = new_block_index;
// Update the inode.
if (ext2_write_inode(fs, inode, inode_index) == -1)
if (ext2_write_inode(fs, inode, inode_index) == -1) {
return -1;
}
}
// Allocate the cache.
@@ -1188,13 +1191,15 @@ static int ext2_set_real_block_index(ext2_filesystem_t *fs, ext2_inode_t *inode,
if (!inode->data.blocks.doubly_indir_block) {
// Allocate a new block.
uint32_t new_block_index = ext2_allocate_block(fs);
if (new_block_index == 0)
if (new_block_index == 0) {
return -1;
}
// Update the index.
inode->data.blocks.doubly_indir_block = new_block_index;
// Update the inode.
if (ext2_write_inode(fs, inode, inode_index) == -1)
if (ext2_write_inode(fs, inode, inode_index) == -1) {
return -1;
}
}
// Allocate the cache.
@@ -1246,13 +1251,15 @@ static int ext2_set_real_block_index(ext2_filesystem_t *fs, ext2_inode_t *inode,
if (!inode->data.blocks.trebly_indir_block) {
// Allocate a new block.
uint32_t new_block_index = ext2_allocate_block(fs);
if (new_block_index == 0)
if (new_block_index == 0) {
return -1;
}
// Update the index.
inode->data.blocks.trebly_indir_block = new_block_index;
// Update the inode.
if (ext2_write_inode(fs, inode, inode_index) == -1)
if (ext2_write_inode(fs, inode, inode_index) == -1) {
return -1;
}
}
// Allocate the cache.
@@ -1391,11 +1398,13 @@ static int ext2_allocate_inode_block(ext2_filesystem_t *fs, ext2_inode_t *inode,
pr_debug("Allocating block with index `%d` for inode with index `%d`.\n", block_index, inode_index);
// Allocate the block.
int real_index = ext2_allocate_block(fs);
if (real_index == -1)
if (real_index == -1) {
return -1;
}
// Associate the real index and the index inside the inode.
if (ext2_set_real_block_index(fs, inode, inode_index, block_index, real_index) == -1)
if (ext2_set_real_block_index(fs, inode, inode_index, block_index, real_index) == -1) {
return -1;
}
// Compute the new blocks count.
uint32_t blocks_count = (block_index + 1) * fs->blocks_per_block_count;
if (inode->blocks_count < blocks_count) {
@@ -1406,8 +1415,9 @@ static int ext2_allocate_inode_block(ext2_filesystem_t *fs, ext2_inode_t *inode,
pr_debug("Setting the block count for inode `%d` to `%d` blocks.\n", inode_index, blocks_count / fs->blocks_per_block_count);
}
// Update the inode.
if (ext2_write_inode(fs, inode, inode_index) == -1)
if (ext2_write_inode(fs, inode, inode_index) == -1) {
return -1;
}
return 0;
}
@@ -1419,12 +1429,14 @@ static int ext2_allocate_inode_block(ext2_filesystem_t *fs, ext2_inode_t *inode,
/// @return the amount of data we read, or negative value for an error.
static ssize_t ext2_read_inode_block(ext2_filesystem_t *fs, ext2_inode_t *inode, uint32_t block_index, uint8_t *buffer)
{
if (block_index >= (inode->blocks_count / fs->blocks_per_block_count))
if (block_index >= (inode->blocks_count / fs->blocks_per_block_count)) {
return -1;
}
// Get the real index.
uint32_t real_index = ext2_get_real_block_index(fs, inode, block_index);
if (real_index == 0)
if (real_index == 0) {
return -1;
}
// Log the address to the inode block.
pr_debug("Read inode block (block:%4u real:%4u)\n", block_index, real_index);
// Read the block.
@@ -1446,8 +1458,9 @@ static ssize_t ext2_write_inode_block(ext2_filesystem_t *fs, ext2_inode_t *inode
}
// Get the real index.
uint32_t real_index = ext2_get_real_block_index(fs, inode, block_index);
if (real_index == 0)
if (real_index == 0) {
return -1;
}
// Log the address to the inode block.
pr_debug("Write inode block (block:%4u real:%4u inode:%4u)\n", block_index, real_index, inode_index);
// Write the block.
@@ -1465,8 +1478,9 @@ static ssize_t ext2_write_inode_block(ext2_filesystem_t *fs, ext2_inode_t *inode
static ssize_t ext2_read_inode_data(ext2_filesystem_t *fs, ext2_inode_t *inode, uint32_t inode_index, off_t offset, size_t nbyte, char *buffer)
{
// Check if the file is empty.
if (inode->size == 0)
if (inode->size == 0) {
return 0;
}
uint32_t end;
if ((offset + nbyte) > inode->size) {
@@ -1692,8 +1706,9 @@ static inline int ext2_directory_is_empty(ext2_filesystem_t *fs, uint8_t *cache,
{
ext2_direntry_iterator_t it = ext2_direntry_iterator_begin(fs, cache, inode);
for (; ext2_direntry_iterator_valid(&it); ext2_direntry_iterator_next(&it)) {
if (it.direntry->inode != 0)
if (it.direntry->inode != 0) {
return 0;
}
}
return 1;
}
@@ -1771,8 +1786,9 @@ static int ext2_allocate_direntry(
// If we hit a direntry with an empty inode, that is a free direntry.
if (it.direntry->inode == 0) {
pr_debug("Found free direntry: %p (%d <= %d)\n", it.direntry, it.direntry->rec_len, rec_len);
if (rec_len <= it.direntry->rec_len)
if (rec_len <= it.direntry->rec_len) {
break;
}
}
// Compute the real rec_len of the entry.
uint32_t real_rec_len = ext2_get_rec_len_from_direntry(it.direntry);
@@ -1896,16 +1912,19 @@ static int ext2_find_direntry(ext2_filesystem_t *fs, ino_t ino, const char *name
ext2_direntry_iterator_t it = ext2_direntry_iterator_begin(fs, cache, &inode);
for (; ext2_direntry_iterator_valid(&it); ext2_direntry_iterator_next(&it)) {
// Skip unused inode.
if (it.direntry->inode == 0)
if (it.direntry->inode == 0) {
continue;
}
// Chehck the name.
if (!strcmp(it.direntry->name, ".") && !strcmp(name, "/")) {
break;
}
// Check if the entry has the same name.
if (strlen(name) == it.direntry->name_len)
if (!strncmp(it.direntry->name, name, it.direntry->name_len))
if (strlen(name) == it.direntry->name_len) {
if (!strncmp(it.direntry->name, name, it.direntry->name_len)) {
break;
}
}
}
// Copy the inode of the parent, even if we did not find the entry.
search->parent_inode = ino;
@@ -1975,8 +1994,9 @@ static int ext2_resolve_path(vfs_file_t *directory, char *path, ext2_direntry_se
return -1;
}
// If the path is `/`.
if (strcmp(path, "/") == 0)
if (strcmp(path, "/") == 0) {
return ext2_find_direntry(fs, directory->ino, path, search);
}
ino_t ino = directory->ino;
char *tmp_path = strdup(path);
char *token = strtok(tmp_path, "/");
@@ -2134,8 +2154,9 @@ static vfs_file_t *ext2_find_vfs_file_with_inode(ext2_filesystem_t *fs, ino_t in
{
// Get the file structure.
file = list_entry(it, vfs_file_t, siblings);
if (file && (file->ino == inode))
if (file && (file->ino == inode)) {
return file;
}
}
}
return NULL;
@@ -2690,12 +2711,14 @@ static ssize_t ext2_getdents(vfs_file_t *file, dirent_t *dirp, off_t doff, size_
ext2_direntry_iterator_t it = ext2_direntry_iterator_begin(fs, cache, &inode);
for (; ext2_direntry_iterator_valid(&it) && (written < count); ext2_direntry_iterator_next(&it)) {
// Skip unused inode.
if (it.direntry->inode == 0)
if (it.direntry->inode == 0) {
continue;
}
// Skip if already provided.
current += sizeof(dirent_t);
if (current <= doff)
if (current <= doff) {
continue;
}
// Write on current directory entry data.
dirp->d_ino = it.direntry->inode;
dirp->d_type = ext2_file_type_to_vfs_file_type(it.direntry->file_type);
+2 -2
View File
@@ -4,11 +4,11 @@
/// See LICENSE.md for details.
#include "fs/ioctl.h"
#include "fs/vfs.h"
#include "process/scheduler.h"
#include "system/printk.h"
#include "stdio.h"
#include "sys/errno.h"
#include "fs/vfs.h"
#include "system/printk.h"
int sys_ioctl(int fd, int request, void *data)
{
+2 -2
View File
@@ -4,9 +4,9 @@
/// See LICENSE.md for details.
#include "process/scheduler.h"
#include "sys/errno.h"
#include "io/debug.h"
#include "fs/vfs.h"
#include "io/debug.h"
#include "sys/errno.h"
int sys_unlink(const char *path)
{
+8 -8
View File
@@ -4,16 +4,16 @@
/// See LICENSE.md for details.
#include "process/scheduler.h"
#include "process/process.h"
#include "system/printk.h"
#include "fcntl.h"
#include "system/syscall.h"
#include "string.h"
#include "limits.h"
#include "io/debug.h"
#include "sys/errno.h"
#include "stdio.h"
#include "fs/vfs.h"
#include "io/debug.h"
#include "limits.h"
#include "process/process.h"
#include "stdio.h"
#include "string.h"
#include "sys/errno.h"
#include "system/printk.h"
#include "system/syscall.h"
int sys_open(const char *pathname, int flags, mode_t mode)
{
+56 -33
View File
@@ -9,14 +9,14 @@
#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level.
#include "io/debug.h" // Include debugging functions.
#include "assert.h"
#include "fcntl.h"
#include "fs/procfs.h"
#include "fs/vfs.h"
#include "libgen.h"
#include "stdio.h"
#include "string.h"
#include "sys/errno.h"
#include "fcntl.h"
#include "libgen.h"
#include "assert.h"
#include "stdio.h"
#include "time.h"
/// Maximum length of name in PROCFS.
@@ -134,9 +134,14 @@ static inline bool_t procfs_check_file(procfs_file_t *procfs_file)
static inline procfs_file_t *procfs_get_file(list_head *entry)
{
procfs_file_t *procfs_file;
if (entry)
if (procfs_check_file((procfs_file = list_entry(entry, procfs_file_t, siblings))))
if (entry) {
// Get the entry.
procfs_file = list_entry(entry, procfs_file_t, siblings);
// Check the file.
if (procfs_file && procfs_check_file(procfs_file)) {
return procfs_file;
}
}
return NULL;
}
@@ -152,8 +157,9 @@ static inline procfs_file_t *procfs_find_entry_path(const char *path)
// Get the file structure.
procfs_file = procfs_get_file(it);
// Check its name.
if (procfs_file && !strcmp(procfs_file->name, path))
if (procfs_file && !strcmp(procfs_file->name, path)) {
return procfs_file;
}
}
}
return NULL;
@@ -162,7 +168,7 @@ static inline procfs_file_t *procfs_find_entry_path(const char *path)
/// @brief Finds the PROCFS file with the given inode.
/// @param inode the inode we search.
/// @return a pointer to the PROCFS file, NULL otherwise.
static inline procfs_file_t *procfs_find_entry_inode(int inode)
static inline procfs_file_t *procfs_find_entry_inode(uint32_t inode)
{
procfs_file_t *procfs_file;
if (!list_head_empty(&fs.files)) {
@@ -185,16 +191,19 @@ static inline procfs_file_t *procfs_find_entry_inode(int inode)
static inline int procfs_find_inode(const char *path)
{
procfs_file_t *procfs_file = procfs_find_entry_path(path);
if (procfs_file)
if (procfs_file) {
return procfs_file->inode;
}
return -1;
}
static inline int procfs_get_free_inode()
{
for (int inode = 1; inode < PROCFS_MAX_FILES; ++inode)
if (procfs_find_entry_inode(inode) == NULL)
for (int inode = 1; inode < PROCFS_MAX_FILES; ++inode) {
if (procfs_find_entry_inode(inode) == NULL) {
return inode;
}
}
return -1;
}
@@ -333,8 +342,9 @@ static void dump_procfs()
// Get the file structure.
file = procfs_get_file(it);
// Check if it a valid procfs file.
if (file)
if (file) {
pr_debug("[%3d] `%s`\n", file->inode, file->name);
}
}
}
}
@@ -580,9 +590,11 @@ static ssize_t procfs_read(vfs_file_t *file, char *buf, off_t offset, size_t nby
{
if (file) {
procfs_file_t *procfs_file = procfs_find_entry_inode(file->ino);
if (procfs_file && procfs_file->dir_entry.fs_operations)
if (procfs_file->dir_entry.fs_operations->read_f)
if (procfs_file && procfs_file->dir_entry.fs_operations) {
if (procfs_file->dir_entry.fs_operations->read_f) {
return procfs_file->dir_entry.fs_operations->read_f(file, buf, offset, nbyte);
}
}
}
return -ENOSYS;
}
@@ -597,9 +609,11 @@ static ssize_t procfs_write(vfs_file_t *file, const void *buf, off_t offset, siz
{
if (file) {
procfs_file_t *procfs_file = procfs_find_entry_inode(file->ino);
if (procfs_file && procfs_file->dir_entry.fs_operations)
if (procfs_file->dir_entry.fs_operations->write_f)
if (procfs_file && procfs_file->dir_entry.fs_operations) {
if (procfs_file->dir_entry.fs_operations->write_f) {
return procfs_file->dir_entry.fs_operations->write_f(file, buf, offset, nbyte);
}
}
}
return -ENOSYS;
}
@@ -623,9 +637,11 @@ off_t procfs_lseek(vfs_file_t *file, off_t offset, int whence)
pr_err("There is no PROCFS fiel associated with the VFS file.\n");
return -ENOSYS;
}
if (procfs_file->dir_entry.fs_operations)
if (procfs_file->dir_entry.fs_operations->lseek_f)
if (procfs_file->dir_entry.fs_operations) {
if (procfs_file->dir_entry.fs_operations->lseek_f) {
return procfs_file->dir_entry.fs_operations->lseek_f(file, offset, whence);
}
}
return -EINVAL;
}
@@ -656,10 +672,10 @@ static int procfs_fstat(vfs_file_t *file, stat_t *stat)
if (file && stat) {
procfs_file_t *procfs_file = procfs_find_entry_inode(file->ino);
if (procfs_file) {
if (procfs_file->dir_entry.fs_operations && procfs_file->dir_entry.fs_operations->stat_f)
if (procfs_file->dir_entry.fs_operations && procfs_file->dir_entry.fs_operations->stat_f) {
return procfs_file->dir_entry.fs_operations->stat_f(file, stat);
else
return __procfs_stat(procfs_file, stat);
}
return __procfs_stat(procfs_file, stat);
}
}
return -ENOSYS;
@@ -674,10 +690,10 @@ static int procfs_stat(const char *path, stat_t *stat)
if (path && stat) {
procfs_file_t *procfs_file = procfs_find_entry_path(path);
if (procfs_file) {
if (procfs_file->dir_entry.sys_operations && procfs_file->dir_entry.sys_operations->stat_f)
if (procfs_file->dir_entry.sys_operations && procfs_file->dir_entry.sys_operations->stat_f) {
return procfs_file->dir_entry.sys_operations->stat_f(path, stat);
else
return __procfs_stat(procfs_file, stat);
}
return __procfs_stat(procfs_file, stat);
}
}
return -1;
@@ -687,9 +703,11 @@ static int procfs_ioctl(vfs_file_t *file, int request, void *data)
{
if (file) {
procfs_file_t *procfs_file = procfs_find_entry_inode(file->ino);
if (procfs_file)
if (procfs_file->dir_entry.fs_operations && procfs_file->dir_entry.fs_operations->ioctl_f)
if (procfs_file) {
if (procfs_file->dir_entry.fs_operations && procfs_file->dir_entry.fs_operations->ioctl_f) {
return procfs_file->dir_entry.fs_operations->ioctl_f(file, request, data);
}
}
}
return -1;
}
@@ -704,27 +722,31 @@ static int procfs_ioctl(vfs_file_t *file, int request, void *data)
/// @return The number of written bytes in the buffer.
static inline ssize_t procfs_getdents(vfs_file_t *file, dirent_t *dirp, off_t doff, size_t count)
{
if (!file || !dirp)
if (!file || !dirp) {
return -1;
}
// Check if the size of the buffer is big enough to hold the data about the
// directory entry.
if (count < sizeof(dirent_t))
if (count < sizeof(dirent_t)) {
return -1;
}
// If there are no file, stop right here.
if (list_head_empty(&fs.files))
if (list_head_empty(&fs.files)) {
return 0;
}
// Find the directory entry.
procfs_file_t *direntry = procfs_find_entry_inode(file->ino);
if (direntry == NULL) {
return -ENOENT;
}
// Check if it is a directory.
if ((direntry->flags & DT_DIR) == 0)
if ((direntry->flags & DT_DIR) == 0) {
return -ENOTDIR;
}
// Clear the buffer.
memset(dirp, 0, count);
// Initialize, the length of the directory name.
int len = strlen(direntry->name);
size_t len = strlen(direntry->name);
ssize_t written_size = 0;
off_t iterated_size = 0;
char parent_path[PATH_MAX];
@@ -746,7 +768,7 @@ static inline ssize_t procfs_getdents(vfs_file_t *file, dirent_t *dirp, off_t do
continue;
}
// Check if the parent of the entry is the directory we are iterating.
if (strcmp(direntry->name, parent_path)) {
if (strcmp(direntry->name, parent_path) != 0) {
continue;
}
// Advance the size we just iterated.
@@ -769,8 +791,9 @@ static inline ssize_t procfs_getdents(vfs_file_t *file, dirent_t *dirp, off_t do
written_size += sizeof(dirent_t);
// Move to next writing position.
++dirp;
if (written_size >= count)
if (written_size >= count) {
break;
}
}
return written_size;
}
+4 -4
View File
@@ -4,12 +4,12 @@
/// See LICENSE.md for details.
#include "process/scheduler.h"
#include "fs/vfs_types.h"
#include "system/panic.h"
#include "sys/errno.h"
#include "fcntl.h"
#include "stdio.h"
#include "fs/vfs.h"
#include "fs/vfs_types.h"
#include "stdio.h"
#include "sys/errno.h"
#include "system/panic.h"
ssize_t sys_read(int fd, void *buf, size_t nbytes)
{
+9 -8
View File
@@ -4,14 +4,14 @@
/// See LICENSE.md for details.
#include "sys/dirent.h"
#include "process/scheduler.h"
#include "system/syscall.h"
#include "system/printk.h"
#include "sys/errno.h"
#include "stdio.h"
#include "fs/vfs.h"
#include "string.h"
#include "assert.h"
#include "fs/vfs.h"
#include "process/scheduler.h"
#include "stdio.h"
#include "string.h"
#include "sys/errno.h"
#include "system/printk.h"
#include "system/syscall.h"
ssize_t sys_getdents(int fd, dirent_t *dirp, unsigned int count)
{
@@ -44,7 +44,8 @@ ssize_t sys_getdents(int fd, dirent_t *dirp, unsigned int count)
// Perform the read.
ssize_t actual_read = vfs_getdents(file, dirp, process_fd->file_struct->f_pos, count);
// Update the offset, only if the value the function returns is positive.
if (actual_read > 0)
if (actual_read > 0) {
process_fd->file_struct->f_pos += actual_read;
}
return actual_read;
}
+2 -2
View File
@@ -4,12 +4,12 @@
/// See LICENSE.md for details.
#include "io/debug.h"
#include "sys/errno.h"
#include "fs/vfs.h"
#include "limits.h"
#include "mem/kheap.h"
#include "stdio.h"
#include "string.h"
#include "limits.h"
#include "sys/errno.h"
int sys_stat(const char *path, stat_t *buf)
{
+15 -14
View File
@@ -4,23 +4,23 @@
/// See LICENSE.md for details.
// Setup the logging for this file (do this before any other include).
#include "sys/kernel_levels.h" // Include kernel log levels.
#define __DEBUG_HEADER__ "[VFS ]" ///< Change header.
#include "sys/kernel_levels.h" // Include kernel log levels.
#define __DEBUG_HEADER__ "[VFS ]" ///< Change header.
#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level.
#include "io/debug.h" // Include debugging functions.
#include "io/debug.h" // Include debugging functions.
#include "fs/vfs.h"
#include "process/scheduler.h"
#include "klib/spinlock.h"
#include "strerror.h"
#include "system/syscall.h"
#include "klib/hashmap.h"
#include "string.h"
#include "fs/procfs.h"
#include "assert.h"
#include "fs/procfs.h"
#include "fs/vfs.h"
#include "klib/hashmap.h"
#include "klib/spinlock.h"
#include "libgen.h"
#include "system/panic.h"
#include "process/scheduler.h"
#include "stdio.h"
#include "strerror.h"
#include "string.h"
#include "system/panic.h"
#include "system/syscall.h"
/// The hashmap that associates a type of Filesystem `name` to its `mount` function;
static hashmap_t *vfs_filesystems;
@@ -94,7 +94,7 @@ super_block_t *vfs_get_superblock(const char *absolute_path)
}
}
#else
int len = strlen(superblock->path);
size_t len = strlen(superblock->path);
if (!strncmp(absolute_path, superblock->path, len)) {
if (len > last_sb_len) {
last_sb_len = len;
@@ -544,8 +544,9 @@ int vfs_destroy_task(task_struct *task)
// Decrease the counter.
--task->fd_list[fd].file_struct->count;
// If counter is zero, close the file.
if (task->fd_list[fd].file_struct->count == 0)
if (task->fd_list[fd].file_struct->count == 0) {
task->fd_list[fd].file_struct->fs_operations->close_f(task->fd_list[fd].file_struct);
}
// Clear the pointer to the file structure.
task->fd_list[fd].file_struct = NULL;
}
+1 -1
View File
@@ -146,7 +146,7 @@ char *cpuid_brand_index(pt_regs *f)
"Mobile Intel Celeron",
NULL };
int bx = (f->ebx & 0xFF);
uint32_t bx = (f->ebx & 0xFF);
if (bx > 0x17) {
bx = 0;
+73 -57
View File
@@ -9,21 +9,21 @@
#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level.
#include "io/debug.h" // Include debugging functions.
#include "hardware/timer.h"
#include "klib/irqflags.h"
#include "process/scheduler.h"
#include "hardware/pic8259.h"
#include "io/port_io.h"
#include "io/video.h"
#include "stdint.h"
#include "mem/kheap.h"
#include "process/wait.h"
#include "drivers/rtc.h"
#include "assert.h"
#include "descriptor_tables/isr.h"
#include "devices/fpu.h"
#include "system/signal.h"
#include "assert.h"
#include "drivers/rtc.h"
#include "hardware/pic8259.h"
#include "hardware/timer.h"
#include "io/port_io.h"
#include "io/video.h"
#include "klib/irqflags.h"
#include "mem/kheap.h"
#include "process/scheduler.h"
#include "process/wait.h"
#include "stdint.h"
#include "sys/errno.h"
#include "system/signal.h"
/// @defgroup picregs Programmable Interval Timer Registers
/// @brief The list of registers used to set the PIT.
@@ -155,8 +155,9 @@ void dynamic_timers_install()
tvec_base_t *base = &cpu_base;
base->timer_ticks = 0;
for (int i = 0; i < TVR_SIZE; ++i)
for (int i = 0; i < TVR_SIZE; ++i) {
list_head_init(base->tv1.vec + i);
}
for (int i = 0; i < TVN_SIZE; ++i) {
list_head_init(base->tv2.vec + i);
@@ -173,8 +174,9 @@ void dynamic_timers_install()
/// Prints used slots of timer vector
static void __print_tvec_slots(tvec_base_t *base, int tv_index)
{
if (tv_index < 0 || tv_index > 5)
if (tv_index < 0 || tv_index > 5) {
return;
}
// Write buffer
char result[TVN_SIZE + 1];
@@ -188,13 +190,15 @@ static void __print_tvec_slots(tvec_base_t *base, int tv_index)
for (int i = 0; i < TVR_SIZE; ++i) {
// New line in order to not clutter the screen
int index = i % TVN_SIZE;
if (i != 0 && index == 0)
if (i != 0 && index == 0) {
pr_debug("\n\t%s", result);
}
if (!list_head_empty(base->tv1.vec + i))
if (!list_head_empty(base->tv1.vec + i)) {
result[index] = '1';
else
} else {
result[index] = '0';
}
}
// The last line
@@ -219,14 +223,15 @@ static void __print_tvec_slots(tvec_base_t *base, int tv_index)
}
for (int i = 0; i < TVN_SIZE; ++i) {
if (list_head_empty(tv->vec + i))
if (list_head_empty(tv->vec + i)) {
result[i] = '0';
else
} else {
result[i] = '1';
}
}
pr_debug("base->tv%d.vec:\n\t%s\n", tv_index, result);
(void) result;
(void)result;
}
/// Dump all timer vector in base
@@ -241,7 +246,7 @@ static inline void __dump_all_tvec_slots(tvec_base_t *base)
/// Select correct timer vector and position inside of it for the input timer
/// index contains the position inside of the tv_index timer vector
static void __find_tvec(tvec_base_t *base, struct timer_list *timer, int *index, int *tv_index)
static void __find_tvec(tvec_base_t *base, struct timer_list *timer, uint32_t *index, uint32_t *tv_index)
{
assert(index && "index is NULL");
assert(tv_index && "tv_index is NULL");
@@ -289,7 +294,7 @@ static void __find_tvec(tvec_base_t *base, struct timer_list *timer, int *index,
/// Add timers into different lists based on their expire time
static void __add_timer_tvec_base(tvec_base_t *base, struct timer_list *timer)
{
int index = 0, tv_index = 0;
uint32_t index = 0, tv_index = 0;
__find_tvec(base, timer, &index, &tv_index);
struct list_head *vec;
@@ -322,7 +327,7 @@ static void __add_timer_tvec_base(tvec_base_t *base, struct timer_list *timer)
/// Remove timer from tvec_base
static void __rem_timer_tvec_base(tvec_base_t *base, struct timer_list *timer)
{
int index = 0, tv_index = 0;
uint32_t index = 0, tv_index = 0;
__find_tvec(base, timer, &index, &tv_index);
// TODO: Check why we do not use vec.
@@ -355,7 +360,7 @@ static void __rem_timer_tvec_base(tvec_base_t *base, struct timer_list *timer)
}
/// Move all timers from tv up one level
static int cascate(tvec_base_t *base, timer_vec *tv, int time_index, int tv_index)
static uint32_t cascate(tvec_base_t *base, timer_vec *tv, uint32_t time_index, int tv_index)
{
if (!list_head_empty(tv->vec + time_index)) {
pr_debug("Relocating timers in tv%d.vec[%d]\n", tv_index, time_index);
@@ -383,7 +388,7 @@ void run_timer_softirq()
unsigned long current_ticks = timer_get_ticks();
while (base->timer_ticks <= current_ticks) {
// Index of the current timer to execute
int current_time_index = base->timer_ticks & TVR_MASK;
uint32_t current_time_index = base->timer_ticks & TVR_MASK;
// If the index is zero then all lists in base->tv1 have been checked, so they are empty
if (!current_time_index) {
@@ -396,16 +401,15 @@ void run_timer_softirq()
// lists are now empty. If so, cascade() is invoked once more to replenish
// base->tv2 with the timers included in a list of base->tv3, and so on.
int tv2_index = (base->timer_ticks >> TIMER_TICKS_BITS(0)) & TVN_MASK;
int tv3_index = (base->timer_ticks >> TIMER_TICKS_BITS(1)) & TVN_MASK;
int tv4_index = (base->timer_ticks >> TIMER_TICKS_BITS(2)) & TVN_MASK;
int tv5_index = (base->timer_ticks >> TIMER_TICKS_BITS(3)) & TVN_MASK;
uint32_t tv2_index = (base->timer_ticks >> TIMER_TICKS_BITS(0)) & TVN_MASK;
uint32_t tv3_index = (base->timer_ticks >> TIMER_TICKS_BITS(1)) & TVN_MASK;
uint32_t tv4_index = (base->timer_ticks >> TIMER_TICKS_BITS(2)) & TVN_MASK;
uint32_t tv5_index = (base->timer_ticks >> TIMER_TICKS_BITS(3)) & TVN_MASK;
if (!cascate(base, &base->tv2, tv2_index, 2) &&
!cascate(base, &base->tv3, tv3_index, 3) &&
!cascate(base, &base->tv4, tv4_index, 4) &&
!cascate(base, &base->tv5, tv5_index, 5))
;
!cascate(base, &base->tv5, tv5_index, 5)) {}
}
// If there are timers to execute in this instant
@@ -518,14 +522,17 @@ typedef struct sleep_data_t {
/// @param data Custom data stored in the timer
void sleep_timeout(unsigned long data)
{
// NOTE: We could modify the sleep_on and make it return the wait_queue_entry_t
// and then store it in the dynamic timer data member instead of the task pid,
// this would remove the need to iterate the sleep queue list.
// TODO: We could modify the sleep_on and make it return the
// wait_queue_entry_t and then store it in the dynamic timer data member
// instead of the task pid, this would remove the need to iterate the sleep
// queue list.
// Cast the data.
sleep_data_t *sleep_data = (sleep_data_t *)data;
// Get the entry.
wait_queue_entry_t *entry = sleep_data->entry;
task_struct *task = entry->task;
// Get the task.
task_struct *task = entry->task;
// Executed entry's wakeup test function
int res = entry->func(entry, 0, 0);
@@ -540,9 +547,9 @@ void sleep_timeout(unsigned long data)
int sys_nanosleep(const timespec *req, timespec *rem)
{
// Probabilmente devi salvare rem da qualche parte, perche' dentro ci va
// messo quanto tempo mancava allo scadere del timer nel caso in cui il
// timer venga interrotto prima da un segnale.
// You probably have to save rem somewhere, because it contains how much
// time left until the timer expires, when the timer is stopped early by a
// signal.
pr_debug("sys_nanosleep([s:%d; ns:%d],...)\n", req->tv_sec, req->tv_nsec);
// Saves pid and rem timespec
@@ -571,13 +578,14 @@ int sys_nanosleep(const timespec *req, timespec *rem)
/// @param pid PID of the process whos associated timer has expired
void alarm_timeout(unsigned long pid)
{
sys_kill(pid, SIGALRM);
struct task_struct *cur = scheduler_get_current_process();
cur->real_timer = NULL;
sys_kill((pid_t)pid, SIGALRM);
// Get the current task.
struct task_struct *task = scheduler_get_current_process();
// Remove the timer.
task->real_timer = NULL;
}
int sys_alarm(int seconds)
unsigned sys_alarm(int seconds)
{
pr_debug("sys_alarm(seconds:%d)\n", seconds);
@@ -585,7 +593,7 @@ int sys_alarm(int seconds)
struct timer_list *timer;
// If there is already a timer running
int result = 0;
unsigned result = 0;
if (current->real_timer != NULL) {
del_timer(current->real_timer);
result = (current->real_timer->expires - timer_get_ticks()) / TICKS_PER_SECOND;
@@ -598,8 +606,9 @@ int sys_alarm(int seconds)
return result;
}
} else {
if (seconds == 0)
if (seconds == 0) {
return 0;
}
// Allocate new timer
timer = (struct timer_list *)kmalloc(sizeof(struct timer_list));
@@ -656,8 +665,9 @@ static void update_task_itimerval(int which, const struct itimerval *val)
int sys_getitimer(int which, struct itimerval *curr_value)
{
// Invalid time domain
if (which < 0 || which > 3)
if (which < 0 || which > 3) {
return EINVAL;
}
struct task_struct *curr = scheduler_get_current_process();
switch (which) {
@@ -678,39 +688,44 @@ int sys_getitimer(int which, struct itimerval *curr_value)
}
// Real timer interval timemout
static void it_real_fn(unsigned long pid)
static void it_real_fn(unsigned long data)
{
struct task_struct *cur = scheduler_get_running_process(pid);
pid_t pid = (pid_t)data;
// Get the current task.
struct task_struct *task = scheduler_get_running_process(pid);
// Send the signal.
sys_kill(pid, SIGALRM);
// If the real incr is not 0 then restart
if (cur->it_real_incr != 0) {
if (task->it_real_incr != 0) {
// Create new timer for process
struct timer_list *real_timer = (struct timer_list *)kmalloc(sizeof(struct timer_list));
cur->real_timer = real_timer;
task->real_timer = real_timer;
init_timer(real_timer);
real_timer->expires = timer_get_ticks() + cur->it_real_incr;
real_timer->expires = timer_get_ticks() + task->it_real_incr;
real_timer->function = &it_real_fn;
real_timer->data = cur->pid;
real_timer->data = task->pid;
add_timer(real_timer);
return;
}
// No more timer
cur->real_timer = NULL;
task->real_timer = NULL;
}
int sys_setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value)
{
// Invalid time domain
if (which < 0 || which > 3)
if (which < 0 || which > 3) {
return EINVAL;
}
// Returns old timer interval
if (old_value != NULL)
if (old_value != NULL) {
sys_getitimer(which, old_value);
}
// Get ticks of interval
unsigned long interval_ticks = new_value->it_interval.tv_sec * TICKS_PER_SECOND;
@@ -720,8 +735,9 @@ int sys_setitimer(int which, const struct itimerval *new_value, struct itimerval
struct task_struct *cur = scheduler_get_current_process();
if (interval_ticks == 0) {
// Removes real_timer
if (which == ITIMER_REAL && cur->real_timer != NULL)
if (which == ITIMER_REAL && cur->real_timer != NULL) {
cur->real_timer = NULL;
}
update_task_itimerval(which, new_value);
return -1;
+12 -8
View File
@@ -4,13 +4,13 @@
/// See LICENSE.md for details.
#include "io/debug.h"
#include "io/port_io.h"
#include "io/ansi_colors.h"
#include "sys/bitops.h"
#include "io/port_io.h"
#include "kernel.h"
#include "string.h"
#include "stdio.h"
#include "math.h"
#include "stdio.h"
#include "string.h"
#include "sys/bitops.h"
/// Serial port for QEMU.
#define SERIAL_COM1 (0x03F8)
@@ -24,8 +24,9 @@ void dbg_putchar(char c)
void dbg_puts(const char *s)
{
while ((*s) != 0)
while ((*s) != 0) {
dbg_putchar(*s++);
}
}
static inline void __debug_print_header(const char *file, const char *fun, int line, short log_level, char *header)
@@ -77,8 +78,9 @@ static inline void __debug_print_header(const char *file, const char *fun, int l
void set_log_level(int level)
{
if ((level >= LOGLEVEL_EMERG) && (level <= LOGLEVEL_DEBUG))
if ((level >= LOGLEVEL_EMERG) && (level <= LOGLEVEL_DEBUG)) {
max_log_level = level;
}
}
int get_log_level()
@@ -93,8 +95,9 @@ void dbg_printf(const char *file, const char *fun, int line, char *header, short
static short new_line = 1;
// Stage 1: FORMAT
if (strlen(format) >= BUFSIZ)
if (strlen(format) >= BUFSIZ) {
return;
}
// Start variabile argument's list.
va_list ap;
@@ -133,8 +136,9 @@ const char *to_human_size(unsigned long bytes)
int i = 0;
double dblBytes = bytes;
if (bytes > 1024) {
for (i = 0; (bytes / 1024) > 0 && i < length - 1; i++, bytes /= 1024)
for (i = 0; (bytes / 1024) > 0 && i < length - 1; i++, bytes /= 1024) {
dblBytes = bytes / 1024.0;
}
}
sprintf(output, "%.02lf %2s", dblBytes, suffix[i]);
return output;
+2 -2
View File
@@ -4,10 +4,10 @@
/// See LICENSE.md for details.
#include "fs/procfs.h"
#include "process/process.h"
#include "sys/errno.h"
#include "io/debug.h"
#include "process/process.h"
#include "string.h"
#include "sys/errno.h"
static ssize_t procfb_read(vfs_file_t *file, char *buf, off_t offset, size_t nbyte)
{
+2 -2
View File
@@ -4,13 +4,13 @@
/// See LICENSE.md for details.
#include "process/process.h"
#include "sys/errno.h"
#include "fs/procfs.h"
#include "io/debug.h"
#include "string.h"
#include "sys/errno.h"
#include "sys/msg.h"
#include "sys/sem.h"
#include "sys/shm.h"
#include "string.h"
extern ssize_t procipc_msg_read(vfs_file_t *file, char *buf, off_t offset, size_t nbyte);
+29 -29
View File
@@ -5,13 +5,13 @@
#include "fs/procfs.h"
#include "process/process.h"
#include "string.h"
#include "libgen.h"
#include "stdio.h"
#include "sys/errno.h"
#include "io/debug.h"
#include "libgen.h"
#include "process/prio.h"
#include "process/process.h"
#include "stdio.h"
#include "string.h"
#include "sys/errno.h"
/// @brief Returns the character identifying the process state.
/// @details
@@ -24,20 +24,13 @@
/// X Dead
static inline char __procr_get_task_state_char(int state)
{
if (state == 0x00) // TASK_RUNNING
return 'R';
if (state == (1 << 0)) // TASK_INTERRUPTIBLE
return 'S';
if (state == (1 << 1)) // TASK_UNINTERRUPTIBLE
return 'D';
if (state == (1 << 2)) // TASK_STOPPED
return 'T';
if (state == (1 << 3)) // TASK_TRACED
return 't';
if (state == (1 << 4)) // EXIT_ZOMBIE
return 'Z';
if (state == (1 << 5)) // EXIT_DEAD
return 'X';
if (state == 0x00) { return 'R'; } // TASK_RUNNING
if (state == (1 << 0)) { return 'S'; } // TASK_INTERRUPTIBLE
if (state == (1 << 1)) { return 'D'; } // TASK_UNINTERRUPTIBLE
if (state == (1 << 2)) { return 'T'; } // TASK_STOPPED
if (state == (1 << 3)) { return 't'; } // TASK_TRACED
if (state == (1 << 4)) { return 'Z'; } // EXIT_ZOMBIE
if (state == (1 << 5)) { return 'X'; } // EXIT_DEAD
return '?';
}
@@ -84,10 +77,11 @@ static inline ssize_t __procr_do_stat(char *buffer, size_t bufsize, task_struct
//(4) ppid %d
// The PID of the parent of this process.
//
if (task->parent)
if (task->parent) {
sprintf(buffer, "%s %d", buffer, task->parent->pid);
else
} else {
strcat(buffer, " 0");
}
//(5) TODO: pgrp %d
// The process group ID of the process.
//
@@ -305,10 +299,11 @@ static inline ssize_t __procr_do_stat(char *buffer, size_t bufsize, task_struct
// or 0, for non-real-time processes (see
// sched_setscheduler(2)).
//
if (task->se.prio >= 100)
if (task->se.prio >= 100) {
strcat(buffer, " 0");
else
} else {
sprintf(buffer, "%s %u", buffer, task->se.prio);
}
//(41) TODO: policy %u (since Linux 2.5.19)
// Scheduling policy (see sched_setscheduler(2)). Decode
// using the SCHED_* constants in linux/sched.h.
@@ -379,29 +374,34 @@ static inline ssize_t __procr_do_stat(char *buffer, size_t bufsize, task_struct
/// @return The number of bytes we read.
static inline ssize_t __procr_read(vfs_file_t *file, char *buffer, off_t offset, size_t nbyte)
{
if (file == NULL)
if (file == NULL) {
return -EFAULT;
}
// Get the entry.
proc_dir_entry_t *entry = (proc_dir_entry_t *)file->device;
if (entry == NULL)
if (entry == NULL) {
return -EFAULT;
}
// Get the task.
task_struct *task = (task_struct *)entry->data;
if (task == NULL)
if (task == NULL) {
return -EFAULT;
}
// Prepare a support buffer.
char support[BUFSIZ];
memset(support, 0, BUFSIZ);
// Call the specific function.
if (strcmp(entry->name, "cmdline") == 0)
if (strcmp(entry->name, "cmdline") == 0) {
__procr_do_cmdline(support, BUFSIZ, task);
else if (strcmp(entry->name, "stat") == 0)
} else if (strcmp(entry->name, "stat") == 0) {
__procr_do_stat(support, BUFSIZ, task);
}
// Copmute the amounts of bytes we want (and can) read.
ssize_t bytes_to_read = max(0, min(strlen(support) - offset, nbyte));
// Perform the read.
if (bytes_to_read > 0)
if (bytes_to_read > 0) {
memcpy(buffer, support + offset, bytes_to_read);
}
return bytes_to_read;
}
+17 -14
View File
@@ -4,13 +4,13 @@
/// See LICENSE.md for details.
#include "fs/procfs.h"
#include "version.h"
#include "process/process.h"
#include "string.h"
#include "stdio.h"
#include "sys/errno.h"
#include "io/debug.h"
#include "hardware/timer.h"
#include "io/debug.h"
#include "process/process.h"
#include "stdio.h"
#include "string.h"
#include "sys/errno.h"
#include "version.h"
static ssize_t procs_do_uptime(char *buffer, size_t bufsize);
@@ -26,28 +26,31 @@ static ssize_t procs_do_stat(char *buffer, size_t bufsize);
static ssize_t procs_read(vfs_file_t *file, char *buf, off_t offset, size_t nbyte)
{
if (file == NULL)
if (file == NULL) {
return -EFAULT;
}
proc_dir_entry_t *entry = (proc_dir_entry_t *)file->device;
if (entry == NULL)
if (entry == NULL) {
return -EFAULT;
}
// Prepare a buffer.
char buffer[BUFSIZ];
memset(buffer, 0, BUFSIZ);
// Call the specific function.
int ret = 0;
if (strcmp(entry->name, "uptime") == 0)
if (strcmp(entry->name, "uptime") == 0) {
ret = procs_do_uptime(buffer, BUFSIZ);
else if (strcmp(entry->name, "version") == 0)
} else if (strcmp(entry->name, "version") == 0) {
ret = procs_do_version(buffer, BUFSIZ);
else if (strcmp(entry->name, "mounts") == 0)
} else if (strcmp(entry->name, "mounts") == 0) {
ret = procs_do_mounts(buffer, BUFSIZ);
else if (strcmp(entry->name, "cpuinfo") == 0)
} else if (strcmp(entry->name, "cpuinfo") == 0) {
ret = procs_do_cpuinfo(buffer, BUFSIZ);
else if (strcmp(entry->name, "meminfo") == 0)
} else if (strcmp(entry->name, "meminfo") == 0) {
ret = procs_do_meminfo(buffer, BUFSIZ);
else if (strcmp(entry->name, "stat") == 0)
} else if (strcmp(entry->name, "stat") == 0) {
ret = procs_do_stat(buffer, BUFSIZ);
}
// Perform read.
ssize_t it = 0;
if (ret == 0) {
+16 -13
View File
@@ -9,18 +9,18 @@
#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level.
#include "io/debug.h" // Include debugging functions.
#include "bits/ioctls.h"
#include "bits/termios-struct.h"
#include "ctype.h"
#include "drivers/keyboard/keyboard.h"
#include "drivers/keyboard/keymap.h"
#include "fs/procfs.h"
#include "bits/ioctls.h"
#include "sys/bitops.h"
#include "io/video.h"
#include "sys/errno.h"
#include "fcntl.h"
#include "fs/procfs.h"
#include "fs/vfs.h"
#include "ctype.h"
#include "io/video.h"
#include "process/scheduler.h"
#include "sys/bitops.h"
#include "sys/errno.h"
/// @brief Prints the ring-buffer.
/// @param rb the ring-buffer to print.
@@ -37,8 +37,9 @@ void print_rb(fs_rb_scancode_t *rb)
static ssize_t procv_read(vfs_file_t *file, char *buf, off_t offset, size_t nbyte)
{
// Stop if the buffer is invalid.
if (buf == NULL)
if (buf == NULL) {
return -1;
}
// Get the currently running process.
task_struct *process = scheduler_get_current_process();
@@ -61,8 +62,9 @@ static ssize_t procv_read(vfs_file_t *file, char *buf, off_t offset, size_t nbyt
int c = keyboard_pop_back();
// Check that it's a valid caracter.
if (c < 0)
if (c < 0) {
return 0;
}
// Keep only the character not the scancode.
c &= 0x00FF;
@@ -78,8 +80,9 @@ static ssize_t procv_read(vfs_file_t *file, char *buf, off_t offset, size_t nbyt
// Pop the previous character in buffer.
fs_rb_scancode_pop_front(rb);
// Delete the previous character on video.
if (flg_echoe)
if (flg_echoe) {
video_putc(c);
}
} else {
// Add the character to the buffer.
fs_rb_scancode_push_front(rb, c);
@@ -88,7 +91,8 @@ static ssize_t procv_read(vfs_file_t *file, char *buf, off_t offset, size_t nbyt
return 1;
}
return 0;
} else if (c == 0x7f) {
}
if (c == 0x7f) {
if (flg_echo) {
video_puts("^[[3~");
}
@@ -98,10 +102,9 @@ static ssize_t procv_read(vfs_file_t *file, char *buf, off_t offset, size_t nbyt
fs_rb_scancode_push_front(rb, '3');
fs_rb_scancode_push_front(rb, '~');
return 0;
} else {
// Add the character to the buffer.
fs_rb_scancode_push_front(rb, c);
}
// Add the character to the buffer.
fs_rb_scancode_push_front(rb, c);
// If echo is activated, output the character to video.
if (flg_echo) {
+21 -13
View File
@@ -4,23 +4,25 @@
/// See LICENSE.md for details.
#include "system/syscall.h"
#include "sys/errno.h"
#include "stdio.h"
#include "io/video.h"
#include "ctype.h"
#include "drivers/keyboard/keyboard.h"
#include "io/video.h"
#include "stdio.h"
#include "string.h"
#include "sys/errno.h"
int atoi(const char *str)
{
// Check the input string.
if (str == NULL)
if (str == NULL) {
return 0;
}
// Initialize sign, the result variable, and two indices.
int sign = (str[0] == '-') ? -1 : +1, result = 0, i;
// Find where the number ends.
for (i = (sign == -1) ? 1 : 0; (str[i] != '\0') && isdigit(str[i]); ++i)
for (i = (sign == -1) ? 1 : 0; (str[i] != '\0') && isdigit(str[i]); ++i) {
result = (result * 10) + str[i] - '0';
}
return sign * result;
}
@@ -44,8 +46,9 @@ long strtol(const char *str, char **endptr, int base)
c = *s++;
} else {
neg = 0;
if (c == '+')
if (c == '+') {
c = *s++;
}
}
if ((base == 0 || base == 16) &&
c == '0' && (*s == 'x' || *s == 'X')) {
@@ -53,8 +56,9 @@ long strtol(const char *str, char **endptr, int base)
s += 2;
base = 16;
}
if (base == 0)
if (base == 0) {
base = c == '0' ? 8 : 10;
}
/*
* Compute the cutoff value between legal numbers and illegal
* numbers. That is the largest legal value, divided by the
@@ -83,16 +87,19 @@ long strtol(const char *str, char **endptr, int base)
cutlim = -cutlim;
}
for (acc = 0, any = 0;; c = (unsigned char)*s++) {
if (isdigit(c))
if (isdigit(c)) {
c -= '0';
else if (isalpha(c))
} else if (isalpha(c)) {
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
else
} else {
break;
if (c >= base)
}
if (c >= base) {
break;
if (any < 0)
}
if (any < 0) {
continue;
}
if (neg) {
if (acc < cutoff || (acc == cutoff && c > cutlim)) {
any = -1;
@@ -115,7 +122,8 @@ long strtol(const char *str, char **endptr, int base)
}
}
}
if (endptr != 0)
if (endptr != 0) {
*endptr = (char *)(any ? s - 1 : str);
}
return (acc);
}
+49 -27
View File
@@ -9,16 +9,16 @@
#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level.
#include "io/debug.h" // Include debugging functions.
#include "io/vga/vga.h"
#include "io/vga/vga_palette.h"
#include "io/vga/vga_mode.h"
#include "io/vga/vga_font.h"
#include "hardware/timer.h"
#include "io/port_io.h"
#include "io/vga/vga.h"
#include "io/vga/vga_font.h"
#include "io/vga/vga_mode.h"
#include "io/vga/vga_palette.h"
#include "io/video.h"
#include "math.h"
#include "stdbool.h"
#include "string.h"
#include "math.h"
/// Attribute Controller index port.
#define AC_INDEX 0x03C0
@@ -111,12 +111,15 @@ static inline char *__get_seg(void)
unsigned int seg;
outportb(GC_INDEX, 6);
seg = (inportb(GC_DATA) >> 2) & 3;
if ((seg == 0) || (seg == 1))
if ((seg == 0) || (seg == 1)) {
return (char *)0xA0000;
if (seg == 2)
}
if (seg == 2) {
return (char *)0xB0000;
if (seg == 3)
}
if (seg == 3) {
return (char *)0xB8000;
}
return (char *)seg;
}
@@ -183,8 +186,9 @@ static inline void __set_plane(unsigned int plane)
static unsigned __current_plane = -1u;
unsigned char pmask;
plane &= 3;
if (__current_plane == plane)
if (__current_plane == plane) {
return;
}
// Store the current plane.
__current_plane = plane;
// Compute the plane mask.
@@ -384,8 +388,9 @@ static inline unsigned int __reverse_bits(char num)
unsigned int reverse_num = 0;
int i;
for (i = 0; i < NO_OF_BITS; i++) {
if ((num & (1 << i)))
if ((num & (1 << i))) {
reverse_num |= 1 << ((NO_OF_BITS - 1) - i);
}
}
return reverse_num;
}
@@ -461,10 +466,11 @@ static inline void __write_pixel_4(int x, int y, unsigned char color)
pmask = 1;
for (plane = 0; plane < 4; ++plane) {
__set_plane(plane);
if (pmask & color)
if (pmask & color) {
__write_byte(off, __read_byte(off) | mask);
else
} else {
__write_byte(off, __read_byte(off) & ~mask);
}
pmask <<= 1;
}
}
@@ -511,15 +517,17 @@ int vga_is_enabled()
int vga_width()
{
if (vga_enable)
if (vga_enable) {
return driver->width;
}
return 0;
}
int vga_height()
{
if (vga_enable)
if (vga_enable) {
return driver->height;
}
return 0;
}
@@ -571,19 +579,22 @@ void vga_draw_line(int x0, int y0, int x1, int y1, unsigned char color)
int err = (dx > dy ? dx : -dy) / 2;
while (true) {
vga_draw_pixel(x0, y0, color);
if ((x0 == x1) && (y0 == y1))
if ((x0 == x1) && (y0 == y1)) {
break;
}
if (dx > dy) {
x0 += sx;
err -= dy;
if (err < 0)
if (err < 0) {
err += dx;
}
y0 += sy;
} else {
y0 += sy;
err -= dx;
if (err < 0)
if (err < 0) {
err += dy;
}
x0 += sx;
}
}
@@ -602,8 +613,9 @@ void vga_draw_circle(int xc, int yc, int r, unsigned char color)
int x = 0;
int y = r;
int p = 3 - 2 * r;
if (!r)
if (!r) {
return;
}
while (y >= x) // only formulate 1/8 of circle
{
vga_draw_pixel(xc - x, yc - y, color); //upper left left
@@ -791,30 +803,36 @@ static int _cursor_state = 0;
inline static void __vga_clear_cursor()
{
for (unsigned cy = 0; cy < driver->font->height; ++cy)
for (unsigned cx = 0; cx < driver->font->width; ++cx)
for (unsigned cy = 0; cy < driver->font->height; ++cy) {
for (unsigned cx = 0; cx < driver->font->width; ++cx) {
vga_draw_pixel(_x + cx, _y + cy, 0);
}
}
}
inline static void __vga_draw_cursor()
{
unsigned char color = (_cursor_state = (_cursor_state == 0)) * _color;
for (unsigned cy = 0; cy < driver->font->height; ++cy)
for (unsigned cx = 0; cx < driver->font->width; ++cx)
for (unsigned cy = 0; cy < driver->font->height; ++cy) {
for (unsigned cx = 0; cx < driver->font->width; ++cx) {
vga_draw_pixel(_x + cx, _y + cy, color);
}
}
}
void vga_putc(int c)
{
if (_cursor_state)
__vga_clear_cursor();
// If the character is '\n' go the new line.
if (c == '\n') {
vga_new_line();
} else if ((c >= 0x20) && (c <= 0x7E)) {
vga_draw_char(_x, _y, c, _color);
if ((_x += driver->font->width) >= driver->width)
if ((_x += driver->font->width) >= driver->width) {
vga_new_line();
}
} else {
return;
}
@@ -836,18 +854,22 @@ void vga_move_cursor(unsigned int x, unsigned int y)
void vga_get_cursor_position(unsigned int *x, unsigned int *y)
{
if (x)
if (x) {
*x = _x / driver->font->width;
if (y)
}
if (y) {
*y = _y / driver->font->height;
}
}
void vga_get_screen_size(unsigned int *width, unsigned int *height)
{
if (width)
if (width) {
*width = driver->width / driver->font->width;
if (height)
}
if (height) {
*height = driver->height / driver->font->height;
}
}
void vga_clear_screen()
+17 -11
View File
@@ -4,12 +4,12 @@
/// See LICENSE.md for details.
#include "io/port_io.h"
#include "io/video.h"
#include "io/vga/vga.h"
#include "stdbool.h"
#include "ctype.h"
#include "string.h"
#include "io/vga/vga.h"
#include "io/video.h"
#include "stdbool.h"
#include "stdio.h"
#include "string.h"
#define HEIGHT 25 ///< The height of the
#define WIDTH 80 ///< The width of the
@@ -282,10 +282,12 @@ void video_get_cursor_position(unsigned int *x, unsigned int *y)
return;
}
#endif
if (x)
if (x) {
*x = __get_x();
if (y)
}
if (y) {
*y = __get_y();
}
}
void video_get_screen_size(unsigned int *width, unsigned int *height)
@@ -296,10 +298,12 @@ void video_get_screen_size(unsigned int *width, unsigned int *height)
return;
}
#endif
if (width)
if (width) {
*width = WIDTH;
if (height)
}
if (height) {
*height = HEIGHT;
}
}
void video_clear()
@@ -365,10 +369,11 @@ void video_shift_one_page_up()
// Decrese the number of scrolled pages, and compute which page must be loaded.
int page_to_load = (STORED_PAGES - (--scrolled_page));
// If we have reached 0, restore the original page.
if (scrolled_page == 0)
if (scrolled_page == 0) {
memcpy(ADDR, original_page, TOTAL_SIZE);
else
} else {
memcpy(ADDR, upper_buffer + (page_to_load * TOTAL_SIZE), TOTAL_SIZE);
}
}
}
@@ -378,8 +383,9 @@ void video_shift_one_page_down(void)
// Increase the number of scrolled pages, and compute which page must be loaded.
int page_to_load = (STORED_PAGES - (++scrolled_page));
// If we are loading the first history page, save the original.
if (scrolled_page == 1)
if (scrolled_page == 1) {
memcpy(original_page, ADDR, TOTAL_SIZE);
}
// Load the specific page.
memcpy(ADDR, upper_buffer + (page_to_load * TOTAL_SIZE), TOTAL_SIZE);
}
+14 -11
View File
@@ -5,22 +5,22 @@
// ============================================================================
// Setup the logging for this file (do this before any other include).
#include "sys/kernel_levels.h" // Include kernel log levels.
#include "sys/kernel_levels.h" // Include kernel log levels.
#define __DEBUG_HEADER__ "[IPCmsg]" ///< Change header.
#define __DEBUG_LEVEL__ LOGLEVEL_DEBUG ///< Set log level.
#include "io/debug.h" // Include debugging functions.
// ============================================================================
#include "process/scheduler.h"
#include "assert.h"
#include "fcntl.h"
#include "process/process.h"
#include "system/panic.h"
#include "sys/errno.h"
#include "sys/msg.h"
#include "process/scheduler.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "assert.h"
#include "stdio.h"
#include "fcntl.h"
#include "sys/errno.h"
#include "sys/msg.h"
#include "system/panic.h"
#include "ipc/ipc.h"
@@ -115,8 +115,9 @@ static inline msq_info_t *__list_find_msq_info_by_id(int msqid)
// Get the current entry.
msq_info = list_entry(it, msq_info_t, list);
// If message queue set is valid, check the id.
if (msq_info && (msq_info->id == msqid))
if (msq_info && (msq_info->id == msqid)) {
return msq_info;
}
}
return NULL;
}
@@ -133,8 +134,9 @@ static inline msq_info_t *__list_find_msq_info_by_key(key_t key)
// Get the current entry.
msq_info = list_entry(it, msq_info_t, list);
// If message queue set is valid, check the id.
if (msq_info && (msq_info->msqid.msg_perm.key == key))
if (msq_info && (msq_info->msqid.msg_perm.key == key)) {
return msq_info;
}
}
return NULL;
}
@@ -188,8 +190,9 @@ static inline void __msq_info_remove_message(msq_info_t *msq_info, struct msg *m
}
}
// If the message is the last of the queue, set the last to NULL.
if (msq_info->msg_last == message)
if (msq_info->msg_last == message) {
msq_info->msg_last = NULL;
}
// Clear the pointer in message.
message->msg_next = NULL;
}
+2 -2
View File
@@ -4,10 +4,10 @@
/// See LICENSE.md for details.
#include "stdio.h"
#include "klib/mutex.h"
#include "klib/stdatomic.h"
#include "sys/errno.h"
#include "sys/reboot.h"
#include "klib/stdatomic.h"
#include "klib/mutex.h"
static void machine_power_off()
{
+2 -1
View File
@@ -83,8 +83,9 @@ static void cvt(double arg, int ndigits, int *decpt, int *sign, char *buf, unsig
*p1 = '1';
(*decpt)++;
if (eflag == 0) {
if (p > buf)
if (p > buf) {
*p = '0';
}
p++;
}
}
+26 -13
View File
@@ -5,8 +5,8 @@
#include "klib/hashmap.h"
#include "assert.h"
#include "string.h"
#include "mem/slab.h"
#include "string.h"
/// @brief Stores information of an entry of the hashmap.
struct hashmap_entry_t {
@@ -84,8 +84,9 @@ unsigned int hashmap_str_hash(const void *_key)
char c;
// This is the so-called "sdbm" hash. It comes from a piece of public
// domain code from a clone of ndbm.
while ((c = *key++))
while ((c = *key++)) {
hash = c + (hash << 6) + (hash << 16) - hash;
}
return hash;
}
@@ -178,9 +179,13 @@ void *hashmap_set(hashmap_t *map, const void *key, void *value)
void *hashmap_get(hashmap_t *map, const void *key)
{
unsigned int hash = map->hash_func(key) % map->size;
for (hashmap_entry_t *x = map->entries[hash]; x; x = x->next)
if (map->hash_comp(x->key, key))
return x->value;
for (hashmap_entry_t *x = map->entries[hash]; x; x = x->next) {
if (map->hash_comp(x->key, key)) {
{
return x->value;
}
}
}
return NULL;
}
@@ -219,35 +224,43 @@ void *hashmap_remove(hashmap_t *map, const void *key)
int hashmap_is_empty(hashmap_t *map)
{
for (unsigned int i = 0; i < map->size; ++i)
if (map->entries[i])
for (unsigned int i = 0; i < map->size; ++i) {
if (map->entries[i]) {
return 0;
}
}
return 1;
}
int hashmap_has(hashmap_t *map, const void *key)
{
unsigned int hash = map->hash_func(key) % map->size;
for (hashmap_entry_t *x = map->entries[hash]; x; x = x->next)
if (map->hash_comp(x->key, key))
for (hashmap_entry_t *x = map->entries[hash]; x; x = x->next) {
if (map->hash_comp(x->key, key)) {
return 1;
}
}
return 0;
}
list_t *hashmap_keys(hashmap_t *map)
{
list_t *l = list_create();
for (unsigned int i = 0; i < map->size; ++i)
for (hashmap_entry_t *x = map->entries[i]; x; x = x->next)
for (unsigned int i = 0; i < map->size; ++i) {
for (hashmap_entry_t *x = map->entries[i]; x; x = x->next) {
list_insert_back(l, x->key);
}
}
return l;
}
list_t *hashmap_values(hashmap_t *map)
{
list_t *l = list_create();
for (unsigned int i = 0; i < map->size; ++i)
for (hashmap_entry_t *x = map->entries[i]; x; x = x->next)
for (unsigned int i = 0; i < map->size; ++i) {
for (hashmap_entry_t *x = map->entries[i]; x; x = x->next) {
list_insert_back(l, x->value);
}
}
return l;
}
+4 -4
View File
@@ -4,12 +4,12 @@
/// See LICENSE.md for details.
#include "system/syscall.h"
#include "libgen.h"
#include "string.h"
#include "limits.h"
#include "assert.h"
#include "mem/paging.h"
#include "io/debug.h"
#include "libgen.h"
#include "limits.h"
#include "mem/paging.h"
#include "string.h"
int dirname(const char *path, char *buffer, size_t buflen)
{
+3 -2
View File
@@ -5,8 +5,8 @@
#include "klib/list.h"
#include "assert.h"
#include "string.h"
#include "mem/slab.h"
#include "string.h"
static inline listnode_t *__node_alloc()
{
@@ -105,7 +105,8 @@ void *list_remove_node(list_t *list, listnode_t *node)
if (list->head == node) {
return list_remove_front(list);
} else if (list->tail == node) {
}
if (list->tail == node) {
return list_remove_back(list);
}
+10 -5
View File
@@ -18,26 +18,30 @@ double round(double x)
double floor(double x)
{
if (x > -1.0 && x < 1.0) {
if (x >= 0)
if (x >= 0) {
return 0.0;
}
return -1.0;
}
int i = (int)x;
if (x < 0)
if (x < 0) {
return (double)(i - 1);
}
return (double)i;
}
double ceil(double x)
{
if (x > -1.0 && x < 1.0) {
if (x <= 0)
if (x <= 0) {
return 0.0;
}
return 1.0;
}
int i = (int)x;
if (x > 0)
if (x > 0) {
return (double)(i + 1);
}
return (double)i;
}
@@ -145,8 +149,9 @@ double ln(double x)
double logx(double x, double y)
{
// Base may not equal 1 or be negative.
if (y == 1.f || y < 0.f || ln(y) == 0.f)
if (y == 1.f || y < 0.f || ln(y) == 0.f) {
return 0.f;
}
return ln(x) / ln(y);
}
+29 -16
View File
@@ -4,10 +4,10 @@
/// See LICENSE.md for details.
#include "io/debug.h"
#include "klib/ndtree.h"
#include "assert.h"
#include "sys/list_head.h"
#include "klib/ndtree.h"
#include "mem/slab.h"
#include "sys/list_head.h"
// ============================================================================
// Tree types.
@@ -86,8 +86,9 @@ void ndtree_node_set_value(ndtree_node_t *node, void *value)
void *ndtree_node_get_value(ndtree_node_t *node)
{
if (node)
if (node) {
return node->value;
}
return NULL;
}
@@ -137,8 +138,9 @@ unsigned int ndtree_node_count_children(ndtree_node_t *node)
void ndtree_node_dealloc(ndtree_node_t *node)
{
if (node)
if (node) {
kfree(node);
}
}
// ============================================================================
@@ -184,8 +186,9 @@ static void __ndtree_tree_dealloc_rec(ndtree_t *tree, ndtree_node_t *node, ndtre
void ndtree_tree_dealloc(ndtree_t *tree, ndtree_tree_node_f node_cb)
{
if (tree && tree->root && node_cb)
if (tree && tree->root && node_cb) {
__ndtree_tree_dealloc_rec(tree, tree->root, node_cb);
}
kfree(tree);
}
@@ -210,8 +213,9 @@ static ndtree_node_t *__ndtree_tree_find_rec(ndtree_t *tree, ndtree_tree_cmp_f c
ndtree_node_t *ndtree_tree_find(ndtree_t *tree, ndtree_tree_cmp_f cmp, void *value)
{
if (tree && tree->root && value)
if (tree && tree->root && value) {
return __ndtree_tree_find_rec(tree, cmp, value, tree->root);
}
return NULL;
}
@@ -224,14 +228,16 @@ ndtree_node_t *ndtree_node_find(ndtree_t *tree, ndtree_node_t *node, ndtree_tree
ndtree_tree_cmp_f cmp_fun = cmp ? cmp : tree->cmp;
// If neither the tree nor the function argument are valid, rollback to the
// default comparison function.
if (cmp_fun == NULL)
if (cmp_fun == NULL) {
cmp_fun = __ndtree_tree_node_cmp_ptr_cb;
}
// Iterate throught the children.
list_for_each_decl(it, &node->children)
{
ndtree_node_t *child = list_entry(it, ndtree_node_t, siblings);
if (cmp_fun(tree, child->value, value) == 0)
if (cmp_fun(tree, child->value, value) == 0) {
return child;
}
}
}
}
@@ -240,8 +246,9 @@ ndtree_node_t *ndtree_node_find(ndtree_t *tree, ndtree_node_t *node, ndtree_tree
unsigned int ndtree_tree_size(ndtree_t *tree)
{
if (tree)
if (tree) {
return tree->size;
}
return 0;
}
@@ -271,10 +278,11 @@ int ndtree_tree_remove_node_with_cb(ndtree_t *tree, ndtree_node_t *node, ndtree_
// Merge the lists.
list_head_append(new_list, &node->children);
}
if (node_cb)
if (node_cb) {
node_cb(tree, node);
else
} else {
ndtree_node_dealloc(node);
}
--tree->size;
return 1;
}
@@ -302,8 +310,9 @@ ndtree_iter_t *ndtree_iter_alloc()
void ndtree_iter_dealloc(ndtree_iter_t *iter)
{
if (iter)
if (iter) {
kfree(iter);
}
}
ndtree_node_t *ndtree_iter_first(ndtree_node_t *node, ndtree_iter_t *iter)
@@ -361,17 +370,21 @@ static void __ndtree_tree_visitor_iter(ndtree_t *tree,
{
assert(tree);
assert(node);
if (enter_fun)
if (enter_fun) {
enter_fun(tree, node);
if (!list_head_empty(&node->children))
}
if (!list_head_empty(&node->children)) {
list_for_each_decl(it, &node->children)
__ndtree_tree_visitor_iter(tree, list_entry(it, ndtree_node_t, siblings), enter_fun, exit_fun);
if (exit_fun)
}
if (exit_fun) {
exit_fun(tree, node);
}
}
void ndtree_tree_visitor(ndtree_t *tree, ndtree_tree_node_f enter_fun, ndtree_tree_node_f exit_fun)
{
if (tree && tree->root)
if (tree && tree->root) {
__ndtree_tree_visitor_iter(tree, tree->root, enter_fun, exit_fun);
}
}
+44 -39
View File
@@ -63,15 +63,17 @@ rbtree_node_t *rbtree_node_create(void *value)
void *rbtree_node_get_value(rbtree_node_t *node)
{
if (node)
if (node) {
return node->value;
}
return NULL;
}
void rbtree_node_dealloc(rbtree_node_t *node)
{
if (node)
if (node) {
kfree(node);
}
}
static int rbtree_node_is_red(const rbtree_node_t *node)
@@ -115,9 +117,11 @@ static int rbtree_tree_node_cmp_ptr_cb(
static void rbtree_tree_node_dealloc_cb(rbtree_t *tree, rbtree_node_t *node)
{
if (tree)
if (node)
if (tree) {
if (node) {
rbtree_node_dealloc(node);
}
}
}
// rbtree_t
@@ -504,41 +508,40 @@ int rbtree_tree_test(rbtree_t *tree, rbtree_node_t *root)
{
int lh, rh;
if (root == NULL)
if (root == NULL) {
return 1;
else {
rbtree_node_t *ln = root->link[0];
rbtree_node_t *rn = root->link[1];
/* Consecutive red links */
if (rbtree_node_is_red(root)) {
if (rbtree_node_is_red(ln) || rbtree_node_is_red(rn)) {
pr_err("Red violation");
return 0;
}
}
lh = rbtree_tree_test(tree, ln);
rh = rbtree_tree_test(tree, rn);
/* Invalid binary search tree */
if ((ln != NULL && tree->cmp(tree, ln, root) >= 0) || (rn != NULL && tree->cmp(tree, rn, root) <= 0)) {
pr_err("Binary tree violation");
return 0;
}
/* Black height mismatch */
if (lh != 0 && rh != 0 && lh != rh) {
pr_err("Black violation");
return 0;
}
/* Only count black links */
if (lh != 0 && rh != 0)
return rbtree_node_is_red(root) ? lh : lh + 1;
else
return 0;
}
rbtree_node_t *ln = root->link[0];
rbtree_node_t *rn = root->link[1];
/* Consecutive red links */
if (rbtree_node_is_red(root)) {
if (rbtree_node_is_red(ln) || rbtree_node_is_red(rn)) {
pr_err("Red violation");
return 0;
}
}
lh = rbtree_tree_test(tree, ln);
rh = rbtree_tree_test(tree, rn);
/* Invalid binary search tree */
if ((ln != NULL && tree->cmp(tree, ln, root) >= 0) || (rn != NULL && tree->cmp(tree, rn, root) <= 0)) {
pr_err("Binary tree violation");
return 0;
}
/* Black height mismatch */
if (lh != 0 && rh != 0 && lh != rh) {
pr_err("Black violation");
return 0;
}
/* Only count black links */
if (lh != 0 && rh != 0) {
return rbtree_node_is_red(root) ? lh : lh + 1;
}
return 0;
}
static void rbtree_tree_print_iter(rbtree_t *tree,
@@ -549,10 +552,12 @@ static void rbtree_tree_print_iter(rbtree_t *tree,
assert(node);
assert(fun);
fun(tree, node);
if (node->link[0])
if (node->link[0]) {
rbtree_tree_print_iter(tree, node->link[0], fun);
if (node->link[1])
}
if (node->link[1]) {
rbtree_tree_print_iter(tree, node->link[1], fun);
}
}
void rbtree_tree_print(rbtree_t *tree, rbtree_tree_node_f fun)
+2 -1
View File
@@ -16,8 +16,9 @@ void spinlock_lock(spinlock_t *spinlock)
if (atomic_set_and_test(spinlock, SPINLOCK_BUSY) == 0) {
break;
}
while (*spinlock)
while (*spinlock) {
cpu_relax();
}
}
}
+18 -11
View File
@@ -5,9 +5,9 @@
#include "string.h"
#include "ctype.h"
#include "fcntl.h"
#include "stdio.h"
#include "stdlib.h"
#include "fcntl.h"
#ifdef __KERNEL__
#include "mem/kheap.h"
@@ -25,8 +25,9 @@ char *strncpy(char *destination, const char *source, size_t num)
// is found before num characters have been copied, destination is padded
// with zeros until a total of num characters have been written to it.
if (num) {
while (--num)
while (--num) {
*destination++ = '\0';
}
}
}
// Pointer to destination is returned.
@@ -35,8 +36,9 @@ char *strncpy(char *destination, const char *source, size_t num)
int strncmp(const char *s1, const char *s2, size_t n)
{
if (!n)
if (!n) {
return 0;
}
while ((--n > 0) && (*s1) && (*s2) && (*s1 == *s2)) {
s1++;
s2++;
@@ -75,8 +77,9 @@ char *strchr(const char *s, int ch)
while (*s && *s != (char)ch) {
s++;
}
if (*s == (char)ch)
if (*s == (char)ch) {
return (char *)s;
}
{
return NULL;
}
@@ -164,8 +167,9 @@ size_t strcspn(const char *string, const char *control)
size_t n;
// Clear out bit map.
for (n = 0; n < 32; n++)
for (n = 0; n < 32; n++) {
map[n] = 0;
}
// Set bits in control map.
while (*ctrl) {
@@ -193,8 +197,9 @@ char *strpbrk(const char *string, const char *control)
int n;
// Clear out bit map.
for (n = 0; n < 32; n++)
for (n = 0; n < 32; n++) {
map[n] = 0;
}
// Set bits in control map.
while (*ctrl) {
@@ -318,8 +323,9 @@ char *strncat(char *s1, const char *s2, size_t n)
s1--;
while (n--) {
if (!(*s1++ = *s2++))
if (!(*s1++ = *s2++)) {
return start;
}
}
*s1 = '\0';
@@ -400,9 +406,8 @@ char *strtok_r(char *str, const char *delim, char **saveptr)
// Determine if a token has been found.
if (str == (char *)s) {
return NULL;
} else {
return str;
}
return str;
}
// Intrinsic functions.
@@ -626,8 +631,9 @@ char *strdup(const char *s)
#else
char *new = malloc(len);
#endif
if (new == NULL)
if (new == NULL) {
return NULL;
}
new[len] = '\0';
return (char *)memcpy(new, s, len);
}
@@ -640,8 +646,9 @@ char *strndup(const char *s, size_t n)
#else
char *new = malloc(len);
#endif
if (new == NULL)
if (new == NULL) {
return NULL;
}
new[len] = '\0';
return (char *)memcpy(new, s, len);
}
+6 -6
View File
@@ -4,12 +4,12 @@
/// See LICENSE.md for details.
#include "io/debug.h"
#include "time.h"
#include "stdio.h"
#include "stddef.h"
#include "io/port_io.h"
#include "hardware/timer.h"
#include "drivers/rtc.h"
#include "hardware/timer.h"
#include "io/port_io.h"
#include "stddef.h"
#include "stdio.h"
#include "time.h"
static const char *str_weekdays[] = { "Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday" };
@@ -91,7 +91,7 @@ tm_t *localtime(const time_t *time)
t /= 24;
// Convert Unix time to date
a = (unsigned int)((4 * t + 102032) / 146097 + 15);
b = (unsigned int)(t + 2442113 + a - (a / 4));
b = (t + 2442113 + a - (a / 4));
c = (20 * b - 2442) / 7305;
d = b - 365 * c - (c / 4);
e = d * 1000 / 30601;
+42 -25
View File
@@ -5,9 +5,9 @@
#include "fs/vfs.h"
#include "ctype.h"
#include "string.h"
#include "io/debug.h"
#include "stdio.h"
#include "string.h"
static int vsscanf(const char *buf, const char *s, va_list ap)
{
@@ -16,18 +16,25 @@ static int vsscanf(const char *buf, const char *s, va_list ap)
char tmp[BUFSIZ];
while (*s && *buf) {
while (isspace(*s))
while (isspace(*s)) {
++s;
}
if (*s == '%') {
++s;
for (; *s; ++s) {
if (strchr("dibouxcsefg%", *s))
if (strchr("dibouxcsefg%", *s)) {
break;
if (*s == '*')
noassign = 1;
else if (isdigit(*s)) {
for (tc = s; isdigit(*s); ++s)
;
}
if (*s == '*') {
{
noassign = 1;
}
} else if (isdigit(*s)) {
for (tc = s; isdigit(*s); ++s) {
{
;
}
}
strncpy(tmp, tc, s - tc);
tmp[s - tc] = '\0';
width = strtol(tmp, NULL, 10);
@@ -35,10 +42,12 @@ static int vsscanf(const char *buf, const char *s, va_list ap)
}
}
if (*s == 's') {
while (isspace(*buf))
while (isspace(*buf)) {
++buf;
if (!width)
}
if (!width) {
width = strcspn(buf, " \t\n\r\f\v");
}
if (!noassign) {
char *string = va_arg(ap, char *);
strncpy(string, buf, width);
@@ -46,48 +55,56 @@ static int vsscanf(const char *buf, const char *s, va_list ap)
}
buf += width;
} else if (*s == 'c') {
while (isspace(*buf))
while (isspace(*buf)) {
++buf;
if (!width)
}
if (!width) {
width = 1;
}
if (!noassign) {
strncpy(va_arg(ap, char *), buf, width);
}
buf += width;
} else if (strchr("duxob", *s)) {
while (isspace(*buf))
while (isspace(*buf)) {
++buf;
if (*s == 'd' || *s == 'u')
}
if (*s == 'd' || *s == 'u') {
base = 10;
else if (*s == 'x')
} else if (*s == 'x') {
base = 16;
else if (*s == 'o')
} else if (*s == 'o') {
base = 8;
else if (*s == 'b')
} else if (*s == 'b') {
base = 2;
}
if (!width) {
if (isspace(*(s + 1)) || *(s + 1) == 0)
if (isspace(*(s + 1)) || *(s + 1) == 0) {
width = strcspn(buf, " \t\n\r\f\v");
else
} else {
width = strchr(buf, *(s + 1)) - buf;
}
}
strncpy(tmp, buf, width);
tmp[width] = '\0';
buf += width;
if (!noassign)
if (!noassign) {
*va_arg(ap, unsigned int *) = strtol(tmp, NULL, base);
}
}
if (!noassign)
if (!noassign) {
++count;
}
width = noassign = 0;
++s;
} else {
while (isspace(*buf))
while (isspace(*buf)) {
++buf;
if (*s != *buf)
}
if (*s != *buf) {
break;
else
++s, ++buf;
}
++s, ++buf;
}
}
return (count);
+19 -13
View File
@@ -5,13 +5,13 @@
#include "math.h"
#include "ctype.h"
#include "string.h"
#include "fcvt.h"
#include "io/video.h"
#include "stdarg.h"
#include "stdbool.h"
#include "stdint.h"
#include "stdio.h"
#include "io/video.h"
#include "fcvt.h"
#include "string.h"
/// Size of the buffer used to call cvt functions.
#define CVTBUFSIZE 500
@@ -34,8 +34,9 @@ static char *_upper_digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static inline int skip_atoi(const char **s)
{
int i = 0;
while (isdigit(**s))
while (isdigit(**s)) {
i = i * 10 + *((*s)++) - '0';
}
return i;
}
@@ -97,16 +98,17 @@ static char *number(char *str, long num, int base, int size, int32_t precision,
}
size -= precision;
if (!(flags & (FLAGS_ZEROPAD | FLAGS_LEFT))) {
while (size-- > 0)
while (size-- > 0) {
*str++ = ' ';
}
}
if (sign) {
*str++ = sign;
}
if (flags & FLAGS_HASH) {
if (base == 8)
if (base == 8) {
*str++ = '0';
else if (base == 16) {
} else if (base == 16) {
*str++ = '0';
*str++ = _digits[33];
}
@@ -368,8 +370,9 @@ static char *flt(char *str, double num, int size, int precision, char fmt, unsig
int n, i;
// Left align means no zero padding.
if (flags & FLAGS_LEFT)
if (flags & FLAGS_LEFT) {
flags &= ~FLAGS_ZEROPAD;
}
// Determine padding and sign char.
c = (flags & FLAGS_ZEROPAD) ? '0' : ' ';
@@ -580,10 +583,11 @@ int vsprintf(char *str, const char *fmt, va_list args)
flags |= FLAGS_UPPERCASE;
break;
case 'a':
if (qualifier == 'l')
if (qualifier == 'l') {
tmp = eaddr(tmp, va_arg(args, unsigned char *), field_width, precision, flags);
else
} else {
tmp = iaddr(tmp, va_arg(args, unsigned char *), field_width, precision, flags);
}
continue;
// Integer number formats - set up the flags and "break".
case 'o':
@@ -612,12 +616,14 @@ int vsprintf(char *str, const char *fmt, va_list args)
tmp = flt(tmp, va_arg(args, double), field_width, precision, *fmt, flags | FLAGS_SIGN);
continue;
default:
if (*fmt != '%')
if (*fmt != '%') {
*tmp++ = '%';
if (*fmt)
}
if (*fmt) {
*tmp++ = *fmt;
else
} else {
--fmt;
}
continue;
}
+12 -6
View File
@@ -148,12 +148,15 @@ static inline block_t *__blkmngr_find_best_fitting(heap_header_t *header, uint32
list_for_each_decl(it, &header->free)
{
block = list_entry(it, block_t, free);
if (!block->is_free)
if (!block->is_free) {
continue;
if (!__blkmngr_does_it_fit(block, size))
}
if (!__blkmngr_does_it_fit(block, size)) {
continue;
if (!best_fitting || (block->size < best_fitting->size))
}
if (!best_fitting || (block->size < best_fitting->size)) {
best_fitting = block;
}
}
return best_fitting;
}
@@ -164,8 +167,9 @@ static inline block_t *__blkmngr_get_previous_block(heap_header_t *header, block
assert(header && "Received a NULL heap header.");
assert(block && "Received null block.");
// If the block is actually the head of the list, return NULL.
if (block->list.prev == &header->list)
if (block->list.prev == &header->list) {
return NULL;
}
return list_entry(block->list.prev, block_t, list);
}
@@ -175,8 +179,9 @@ static inline block_t *__blkmngr_get_next_block(heap_header_t *header, block_t *
assert(header && "Received a NULL heap header.");
assert(block && "Received null block.");
// If the block is actually the tail of the list, return NULL.
if (block->list.next == &header->list)
if (block->list.next == &header->list) {
return NULL;
}
return list_entry(block->list.next, block_t, list);
}
@@ -284,8 +289,9 @@ static void *__do_brk(vm_area_struct_t *heap, uint32_t increment)
/// @return Pointer to the allocated memory area.
static void *__do_malloc(vm_area_struct_t *heap, size_t size)
{
if (size == 0)
if (size == 0) {
return NULL;
}
// Get the heap header.
heap_header_t *header = (heap_header_t *)heap->vm_start;
// Calculate size that's used, round it to multiple of 16.
+33 -28
View File
@@ -9,19 +9,19 @@
#define __DEBUG_LEVEL__ LOGLEVEL_DEBUG ///< Set log level.
#include "io/debug.h" // Include debugging functions.
#include "assert.h"
#include "descriptor_tables/isr.h"
#include "mem/kheap.h"
#include "mem/paging.h"
#include "mem/vmem_map.h"
#include "mem/zone_allocator.h"
#include "mem/kheap.h"
#include "descriptor_tables/isr.h"
#include "system/panic.h"
#include "sys/list_head_algorithm.h"
#include "stddef.h"
#include "stdint.h"
#include "sys/list_head.h"
#include "sys/mman.h"
#include "assert.h"
#include "string.h"
#include "sys/list_head.h"
#include "sys/list_head_algorithm.h"
#include "sys/mman.h"
#include "system/panic.h"
/// Cache for storing mm_struct.
kmem_cache_t *mm_cache;
@@ -218,8 +218,9 @@ inline vm_area_struct_t *find_vm_area(mm_struct_t *mm, uint32_t vm_start)
{
segment = list_entry(it, vm_area_struct_t, vm_list);
assert(segment && "There is a NULL area in the list.");
if (segment->vm_start == vm_start)
if (segment->vm_start == vm_start) {
return segment;
}
}
return NULL;
}
@@ -343,16 +344,21 @@ static void __page_fault_panic(pt_regs *f, uint32_t addr)
pr_err("Page fault: 0x%x\n", addr);
pr_err("Possible causes: [ ");
if (!(f->err_code & ERR_PRESENT))
if (!(f->err_code & ERR_PRESENT)) {
pr_err("Page not present ");
if (f->err_code & ERR_RW)
}
if (f->err_code & ERR_RW) {
pr_err("Page is read only ");
if (f->err_code & ERR_USER)
}
if (f->err_code & ERR_USER) {
pr_err("Page is privileged ");
if (f->err_code & ERR_RESERVED)
}
if (f->err_code & ERR_RESERVED) {
pr_err("Overwrote reserved bits ");
if (f->err_code & ERR_INST)
}
if (f->err_code & ERR_INST) {
pr_err("Instruction fetch ");
}
pr_err("]\n");
dbg_print_regs(f);
@@ -404,19 +410,18 @@ static page_table_t *__mem_pg_entry_alloc(page_dir_entry_t *entry, uint32_t flag
entry->accessed = 0;
entry->available = 1;
return kmem_cache_alloc(pgtbl_cache, GFP_KERNEL);
} else {
entry->present |= (flags & MM_PRESENT) != 0;
entry->rw |= (flags & MM_RW) != 0;
// We should not remove a global flag from a page directory,
// if this happens there is probably a bug in the kernel
assert(!entry->global || (flags & MM_GLOBAL));
entry->global &= (flags & MM_GLOBAL) != 0;
entry->user |= (flags & MM_USER) != 0;
return (page_table_t *)get_lowmem_address_from_page(
get_page_from_physical_address(((uint32_t)entry->frame) << 12U));
}
entry->present |= (flags & MM_PRESENT) != 0;
entry->rw |= (flags & MM_RW) != 0;
// We should not remove a global flag from a page directory,
// if this happens there is probably a bug in the kernel
assert(!entry->global || (flags & MM_GLOBAL));
entry->global &= (flags & MM_GLOBAL) != 0;
entry->user |= (flags & MM_USER) != 0;
return (page_table_t *)get_lowmem_address_from_page(
get_page_from_physical_address(((uint32_t)entry->frame) << 12U));
}
static inline void __set_pg_entry_frame(page_dir_entry_t *entry, page_table_t *table)
@@ -630,7 +635,7 @@ void mem_upd_vm_area(page_directory_t *pgd,
if (flags & MM_UPDADDR) {
it.entry->frame = phy_pfn++;
// Flush the tlb to allow address update
// TODO: Check if it's always needed (ex. when the pgdir is not the current one)
// TODO(enrico): Check if it's always needed (ex. when the pgdir is not the current one)
paging_flush_tlb_single(it.pfn * PAGE_SIZE);
}
__set_pg_table_flags(it.entry, flags);
@@ -666,7 +671,7 @@ void mem_clone_vm_area(page_directory_t *src_pgd,
}
// Flush the tlb to allow address update
// TODO: Check if it's always needed (ex. when the pgdir is not the current one)
// TODO(enrico): Check if it's always needed (ex. when the pgdir is not the current one)
paging_flush_tlb_single(dst_it.pfn * PAGE_SIZE);
}
}
@@ -677,7 +682,7 @@ mm_struct_t *create_blank_process_image(size_t stack_size)
mm_struct_t *mm = kmem_cache_alloc(mm_cache, GFP_KERNEL);
memset(mm, 0, sizeof(mm_struct_t));
// TODO: Use this field
// TODO(enrico): Use this field
list_head_init(&mm->mm_list);
page_directory_t *pdir_cpy = kmem_cache_alloc(pgdir_cache, GFP_KERNEL);
+10 -6
View File
@@ -9,10 +9,10 @@
#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level.
#include "io/debug.h" // Include debugging functions.
#include "mem/zone_allocator.h"
#include "assert.h"
#include "mem/paging.h"
#include "mem/slab.h"
#include "assert.h"
#include "mem/zone_allocator.h"
/// @brief Use it to manage cached pages.
typedef struct kmem_obj {
@@ -144,8 +144,9 @@ static inline void *__kmem_cache_alloc_slab(kmem_cache_t *cachep, page_t *slab_p
// Get the element from the kmem_obj object
void *elem = ADDR_FROM_KMEM_OBJ(cachep, obj);
if (cachep->ctor)
if (cachep->ctor) {
cachep->ctor(elem);
}
return elem;
}
@@ -199,8 +200,9 @@ kmem_cache_t *kmem_cache_create(
kmem_fun_t dtor)
{
kmem_cache_t *cachep = (kmem_cache_t *)kmem_cache_alloc(&kmem_cache, GFP_KERNEL);
if (!cachep)
if (!cachep) {
return cachep;
}
__kmem_cache_create(cachep, name, size, align, flags, ctor, dtor, KMEM_START_OBJ_COUNT);
@@ -236,8 +238,9 @@ void *kmem_cache_alloc(kmem_cache_t *cachep, gfp_t flags)
{
if (list_head_empty(&cachep->slabs_partial)) {
if (list_head_empty(&cachep->slabs_free)) {
if (flags == 0)
if (flags == 0) {
flags = cachep->flags;
}
// Refill the cache in an exponential fashion, capping at KMEM_MAX_REFILL_OBJ_COUNT to avoid
// too big allocations
@@ -286,8 +289,9 @@ void kmem_cache_free(void *ptr)
#ifdef ENABLE_CACHE_TRACE
pr_notice("CHACE-FREE 0x%p in %-20s at %s:%d\n", ptr, cachep->name, file, line);
#endif
if (cachep->dtor)
if (cachep->dtor) {
cachep->dtor(ptr);
}
kmem_obj *obj = KMEM_OBJ(cachep, ptr);
+3 -2
View File
@@ -10,8 +10,8 @@
#include "io/debug.h" // Include debugging functions.
#include "mem/vmem_map.h"
#include "system/panic.h"
#include "string.h"
#include "system/panic.h"
/// Virtual addresses manager.
static virt_map_page_manager_t virt_default_mapping;
@@ -88,8 +88,9 @@ static virt_map_page_t *_alloc_virt_pages(uint32_t pfn_count)
uint32_t virt_map_physical_pages(page_t *page, int pfn_count)
{
virt_map_page_t *vpage = _alloc_virt_pages(pfn_count);
if (!vpage)
if (!vpage) {
return 0;
}
uint32_t virt_address = VIRT_PAGE_TO_ADDRESS(vpage);
uint32_t phy_address = get_physical_address_from_page(page);
+7 -6
View File
@@ -9,13 +9,13 @@
#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level.
#include "io/debug.h" // Include debugging functions.
#include "mem/zone_allocator.h"
#include "mem/buddysystem.h"
#include "sys/list_head.h"
#include "kernel.h"
#include "assert.h"
#include "kernel.h"
#include "mem/buddysystem.h"
#include "mem/paging.h"
#include "mem/zone_allocator.h"
#include "string.h"
#include "sys/list_head.h"
/// TODO: Comment.
#define MIN_PAGE_ALIGN(addr) ((addr) & (~(PAGE_SIZE - 1)))
@@ -75,8 +75,9 @@ static zone_t *get_zone_from_page(page_t *page)
last_page = zone->zone_mem_map + zone->size;
assert(last_page && "Failed to retrieve the last page of the zone.");
// Check if the page is before the last page of the zone.
if (page < last_page)
if (page < last_page) {
return zone;
}
}
// Error: page is over memory size.
return (zone_t *)NULL;
@@ -193,7 +194,7 @@ static int pmm_check()
for (; j < 20; ++j) {
free_pages_lowmem((uint32_t)ptr[j]);
}
free_page_lowmem((uint32_t)ptr1);
free_page_lowmem(ptr1);
if (!is_memory_clean(GFP_KERNEL)) {
pr_emerg("Test failed, memory is not clean.\n");

Some files were not shown because too many files have changed in this diff Show More