JustKernel

Ray Of Hope

Basic C

basic programs to clear the concepts

Userspace and kernel space virtual address space division. TO understand the address space allocation between user space and kernel space I wrote this simple program. Kernel space: 10 int init_module(void) 11 { 12 void * kern_mem; 13 kern_mem = kmalloc(5, GFP_KERNEL); 14 printk(KERN_INFO “Hello world addr=%lx”, (unsigned long)kern_mem); 15 return 0; 16 } O/P : […]

Posted in Basic C | 1 Comment »

bitwise operations

Assignment to 4 bit #include <stdio.h> typedef unsigned char uint8; struct nib { uint8 an_term_tp:4, cn_term_tp:4; }; void main() { int an_term = 1; int cn_term = 2; uint8 ch; struct nib nib_obj; ch = *(char *)(int *)&an_term; printf(“%d\n “, ch); if (ch == 1) printf(“\n little endian”); nib_obj.an_term_tp = (char)(an_term) & 0x0f; nib_obj.cn_term_tp = […]

Tags:

Posted in Basic C | No Comments »

JIT+JVM internals

Like most virtual machines, the java virtual machine has a stack- based on architecture akin to microcontroller or microprocessor. The jvm which is the instance of jre, comes into action when java program is executed. When execution is complete, this instance is garbage collected. JIT is a part of JVM that is used to speed […]

Tags:

Posted in Basic C | No Comments »

Structure Padding

Most processors require specific memory alignment on variables certain types. Normally the minimum alignment is the size of the basic type in question, fo instance this is common char variables can be byte aligned and appear at any byte boundary short (2 byte) variables must be 2 byte aligned, they can appear at any even […]

Tags:

Posted in Basic C | No Comments »

Deadlock scenario while handling System/Device Power states.

Recently ran into a deadlock condition where in my USB driver was waiting for reference count to be decremented and the completion routine which decrements the reference count has returned STATUS PENDING. The scenario goes like this: I keep a reference count which is incremented when a request is received (Dispatched ) and is decremented […]

Tags: ,

Posted in Basic C | No Comments »

Protect your allocated memory – Self sustaining Program to detect its own Memory corruptions and leaks

Overrun , Under-run and corruption of the allocated buffers is the common How can you protect these. One of the basic query is how I will detect that my thread has written beyond the allocated buffer at run time. You cant trap the writes to the memory. Even if you trying doing so, then which […]

Tags: ,

Posted in Basic C | No Comments »

To get stacktrace for the program in execution.

Many times its required to get the stack trace during the execution of a program eg . you suspect a particular point in the code to be a possible crash point or whenever you create or kill a thread you want a stack trace or normally you want stack trace at different points of a […]

Posted in Basic C | No Comments »

A risky affair

Does, passing the address as a variable and then using it it to decipher the value is a risky affair ? and should it not be preferred ? These are the questions which I am thinking and trying to find out answers for.. Just giving you a simple example to explain what I meant.. void […]

Posted in Basic C | No Comments »

Return local pointer from function

Return local pointer from function. Is it safe.. It can’t be… You declare a pointer in the function, whose scope of life is only till the function lasts. So a pointer points a valid local variable, till the control is in the local function. As soon as the control is released from the function, the […]

Posted in Basic C | No Comments »

Function Pointer/ Pointers to Function

Pointers to Function / Function Pointers. A useful technique is the ability to have pointers to functions (Specially useful in case of implemening State Machine ). Suppose you declare int func (int , int); The pointer to a function will look like int (*funcptr)(int a, int b ) //pointer to a funtion returning int. Note: […]

Tags:

Posted in Basic C | No Comments »