Move some code to kernel.

Signed-off-by: crynobone <crynobone@gmail.com>