Commit 70773c34 authored by Jithu Joseph's avatar Jithu Joseph Committed by Vikram Narayanan
Browse files

The previous variable argument approch for printf based on 32bit XV6 was not...

The previous variable argument approch for printf based on 32bit XV6 was not working, as the first 6 integer arguments are passed in registers and merely incrementing the sp++ wont fetch subsequent arguments when compiled for 64 bit. changed this to to use builtin va_* macros. Now able to print multiple integers.

Suspecting some issues in loading - see the description of commented printint. This needs to be analysed. (an alternate approch is working is added for time-being)
parent 3c7979bf
......@@ -14,8 +14,14 @@
#include <asm/vmx.h>
#include "../lcd_defs.h"
typedef unsigned int uint;
//#define va_arg(v,l) __builtin_va_arg(v,l)
//#define va_start(v,l) __builtin_va_start(v,l)
//#define va_list __builtin_va_list
typedef unsigned int uint;
// The idea is to get the shared page which is
// to be used for IPC . It is the lowest page
// the bottom of the 4 Page Stack block.
......@@ -34,7 +40,7 @@ strlen(const char *s)
int n;
for(n = 0; s[n]; n++)
;
;
return n;
}
......@@ -54,7 +60,7 @@ static int lcd_puts(const char *buf)
return ret;
}
static int putc(int fd, const char c)
static int abc_putc(int fd, const char c)
{
long ret;
......@@ -70,17 +76,27 @@ static int putc(int fd, const char c)
return ret;
}
static inline void
static void
lcd_putc(char c)
{
asm volatile("movzx %0,%%rax" : : "r" (c));
asm volatile("vmcall");
// printk(KERN_ERR "%c", c);
}
#if 0
// Need to see if there is any issue with
// loading ... This code is faulting
// lcd: page fault VA 0000000000000028
// RIP 0xffffffffa02e2017 ... The culprit
// has to be access to digits[] = "0123456789ABCDEF"
// A slightly different logic below the ifdef .works
// The same works in a regular kernel module
static void
printint(int fd, int xx, int base, int sgn)
printint(int xx, int base, int sgn)
{
static char digits[] = "0123456789ABCDEF";
char digits[] = "0123456789ABCDEF";
char buf[16];
int i, neg;
uint x;
......@@ -98,50 +114,114 @@ printint(int fd, int xx, int base, int sgn)
buf[i++] = digits[x % base];
}while((x /= base) != 0);
if(neg)
buf[i++] = '-';
buf[i++] = '-';
while(--i >= 0)
lcd_putc(buf[i]);
lcd_putc(buf[i]);
}
#endif
#if 1
static void
printint(int xx, int base, int sgn)
{
char buf[16];
int i, neg;
int int_digs[16];
uint x;
neg = 0;
if(sgn && xx < 0){
neg = 1;
x = -xx;
} else {
x = xx;
}
i = 0;
do{
int_digs[i++] = x % base;
}while((x /= base) != 0);
if(neg)
buf[i++] = '-';
while(--i >= 0) {
switch(int_digs[i]) {
case 0: lcd_putc('0');
break;
case 1: lcd_putc('1');
break;
case 2: lcd_putc('2');
break;
case 3: lcd_putc('3');
break;
case 4: lcd_putc('4');
break;
case 5: lcd_putc('5');
break;
case 6: lcd_putc('6');
break;
case 7: lcd_putc('7');
break;
case 8: lcd_putc('8');
break;
case 9: lcd_putc('9');
break;
case 10: lcd_putc('a');
break;
case 11: lcd_putc('b');
break;
case 12: lcd_putc('c');
break;
case 13: lcd_putc('d');
break;
case 14: lcd_putc('e');
break;
case 15: lcd_putc('f');
break;
default:
lcd_putc('-');
break;
}
}
}
#endif
// Print to the given fd. Only understands %d, %x, %p, %s.
void
printf(int fd, char *fmt, ...)
my_printf(char *fmt, ...)
{
va_list ap;
char *s;
int c, i, state;
uint *ap;
va_start(ap, fmt);
state = 0;
ap = (uint*)(void*)&fmt + 1;
for(i = 0; fmt[i]; i++){
c = fmt[i] & 0xff;
if(state == 0){
if(c == '%'){
state = '%';
} else {
// lcd_putc(fd, c);
lcd_putc(c);
}
} else if(state == '%'){
if(c == 'd'){
printk(KERN_ERR "printk The int is %d\n", *ap);
printint(fd, *ap, 10, 1);
ap++;
printint(va_arg(ap, int), 10, 1);
} else if(c == 'x' || c == 'p'){
printint(fd, *ap, 16, 0);
ap++;
printint(va_arg(ap, int), 16, 0);
} else if(c == 's'){
s = (char*)*ap;
ap++;
s = va_arg(ap, char*);
if(s == 0)
s = "(null)";
s = "(null)";
while(*s != 0){
lcd_putc(*s);
s++;
}
} else if(c == 'c'){
lcd_putc( *ap);
ap++;
lcd_putc(va_arg(ap, uint));
} else if(c == '%'){
lcd_putc(c);
} else {
......@@ -154,14 +234,12 @@ printf(int fd, char *fmt, ...)
}
}
void temp_fn() {
char *ptr;
// int check = 107;
// printk(KERN_ERR "printk2 %d\n", check);
printf(1, "temp func \n");
void temp_fn(int var) {
int check = 107;
int hex = 0xdeadbeef;
my_printf("Temp %d here %d and %x\n", check, var, hex);
ptr = get_shared();
*ptr = 'J';
}
......@@ -169,11 +247,11 @@ static volatile int shared_var = 0;
static int hello_2_init(void)
{
int check = 107;
//printk(KERN_INFO "Hello, world 2\n");
if (shared_var == 0) {
lcd_struct *lcd;
if (shared_var == 0) {
shared_var = 1;
lcd_struct *lcd = lcd_create();
lcd = lcd_create();
lcd_move_module(lcd, &__this_module);
lcd_run(lcd);
printk("%d\n", shared_var);
......@@ -182,9 +260,9 @@ static int hello_2_init(void)
}
// guest
shared_var = 2;
//printk(KERN_ERR "printk2 %d\n", check);
printf(1, "Hellow World\n");
temp_fn();
my_printf("ozzie\n");
temp_fn(69);
return 0;
}
......@@ -193,9 +271,9 @@ static void __exit hello_2_exit(void)
{
printk(KERN_INFO "Goodbye, world 2\n");
// printf(1, "goodbye World\n");
;
}
module_init(hello_2_init);
module_exit(hello_2_exit);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment