Cavity formation in semiconductor lasers